Search
 
SCRIPT & CODE EXAMPLE
 

DART

how to use same bloc in multiple widgets in flutter

class CounterPage extends StatefulWidget {
  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  CounterBloc counterBloc;

  @override
  void initState() {
    super.initState();
    counterBloc = BlocProvider.of<CounterBloc>(context);
  }

  @override
  void dispose() {
    counterBloc.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: BlocBuilder<CounterBloc, int>(
        builder: (_, count) {
          return Column(
            children: <Widget>[
              Text(
                '$count',
                style: const TextStyle(fontSize: 24.0),
              ),
              RaisedButton(
                child: Text("Recreating state"),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => BlocProvider<CounterBloc>.value(
                        value: counterBloc,
                        child: ThirdPage(),
                      ),
                    ),
                  );
                },
              ),
              RaisedButton(
                child: Text("Getting errorBlocProvider.of() called  "),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => BlocProvider<CounterBloc>.value(
                        value: counterBloc,
                        child: FourthPage(),
                      ),
                    ),
                  );
                },
              )
            ],
          );
        },
      ),
      // ....
    );
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: collection for in dart 
Dart :: flutter fittedbox max value 
Dart :: is init state executed when returning with navigator flutter 
Dart :: how to use $ input in dart as a string 
Dart :: flutter getit short 
Dart :: onpressed flutter calculate 
Dart :: flutter add external icons 
Dart :: how to wait until result of async is returned dart 
Dart :: tab color in flutter 
Dart :: flutter sqflite foreign keyy 
Dart :: flutter how to load a future function in main function 
Dart :: flutter sliver app bar remove top padding 
Dart :: flutter instance of 
Dart :: This constructor cannot be used in null-safe code. Use [List.filled] to create a non-empty list. 
Dart :: import intl in flutter 
Swift :: format decimal place swift 
Swift :: navigationview hide header swiftui 
Swift :: get device height and width wift 
Swift :: firebase nil value equals 
Swift :: swiftui slider 
Swift :: swift temporary directory 
Swift :: swift convert data to dictionary 
Swift :: Properties Swift 
Swift :: append new element to dictionary in swift 
Swift :: and in swif 
Swift :: swift print 
Swift :: swift completion handler 
Swift :: init with bindings swiftui 
Swift :: sf symbols 
Swift :: Swift Nested function 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =