Landy Cuadra
Landy Cuadra

Reputation: 165

How to use provider beetween several classes in flutter?

I am creating a register app in flutter, but there is too much information tu use a single screen, so I decided to use tab views in the screens and separate the screens in objects of a drawer.

but I needed to use the information from diferent screens to saved it. so I think about using a Multiprovider. but when I try to use the information in the provider it throws:

Could not find the correct Provider<General> above this Baseformularios Widget

my implementation is like this:

Here is my drawer with the first field General, that is basic information about the client, as you can see the provider is created here and the body: is supplied by a variable called bodycontent, that will be over written when a try to change the form, but when I try to print the name of the user it throws the error above.

MultiProvider(
      providers: [
        ChangeNotifierProvider<General>(
          create: (context)=>General(),
        )
      ],
      child: Scaffold(
        appBar: AppBar(
          title: Text("Historias clinicas"),
          centerTitle: true,
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.check),
              onPressed: (){
                print(Provider.of<General>(context).nombre);
              Provider.of<General>(context).addCLiente();
              },
            )
          ],
        ),
        body: bodycontent,
        drawer: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              AspectRatio(
                aspectRatio: 0.001/0.001,
                child:  DrawerHeader(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(colors: [Colors.white,Colors.blueAccent]
                  ),
                ),
              ),

              ListTile(
                title: Text("General"),
                onTap: (){
                  setState(() {
                    bodycontent = I_II_III_IV();
                  });
                  Navigator.pop(context);
                },
              ),
            ],
          ),
        ),
      ),
    );

and here is the usage on the form general:

Provider.of<General>(context).set(nombre.text,
        "Apellido hay que quitar en la funcion",
        edad.text,
        _currentsexo,
        estado_civil.text,
        direccion.text,
        emergencia.text,
        procedencia.text,
        telefono.text,
        ocupacion.text,
        referencia.text,
        fecha_inicio.text,
        "",userid);

this save well the information, I checked it in the debugger, but the problem is in the other side.

I tried using the multiprovider in the screen before the one with the drawer, but it throwed the same error but with "above generalform". notice that I removed the multiprovider from the screen with the drawer when I tried this.

so should I declare the multiprovider in every screen to use it into it's son? because I supposed that provider where variables that you could use in every screen after the one where it's declare, or am I missunderstanding something?... Every help is very appreciated, thanks before hand.

Upvotes: 0

Views: 367

Answers (1)

Daniel
Daniel

Reputation: 1079

To get access to the Provider from every where in your app, you should declare it in the main:

void main()  {
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider<General>(
        create: (context) => General(),
      ),
    ],
    child: MyApp(),
  ));
}

Upvotes: 1

Related Questions