brianegan/scoped_model

bug in scopedModelDescendant

Closed this issue · 1 comments

In my other code that code is correct and i put the same code and have a error in scopedmodel.

import 'package:armarios_inteligentes/models/user_model.dart';
import 'package:armarios_inteligentes/widgets/sign_up_button.dart';
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

class NewLoginScreen extends StatefulWidget {
  @override
  _NewLoginScreenState createState() => _NewLoginScreenState();
}

class _NewLoginScreenState extends State<NewLoginScreen> {
  final _emailController = TextEditingController();
  final _passController = TextEditingController();

  final _formKey = GlobalKey<FormState>();
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(backgroundColor: Colors.transparent),
      body: ScopedModelDescendant<UserModel>(
        builder: (context, child, model) {
          if (model.isLoading)
            return Center(
              child: CircularProgressIndicator(),
            );
          return Form(
            key: _formKey,
            child: Container(
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("images/blue2.jpg"),
                      fit: BoxFit.cover)),
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  Stack(
                    alignment: Alignment.bottomCenter,
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.only(top: 10, bottom: 10),
                            child: Image.asset(
                              "images/logo.png",
                              width: 130,
                              height: 130,
                              fit: BoxFit.contain,
                            ),
                          ),
                          TextFormField(
                            style: TextStyle(color: Colors.white),
                            controller: _emailController,
                            decoration: InputDecoration(
                              hintStyle: TextStyle(
                                  color: Colors.white,
                                  fontFamily: "WorkSansLight",
                                  fontSize: 15.0),
                              filled: true,
                              fillColor: Colors.white24,
                              hintText: "E-mail",
                              border: OutlineInputBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(90.0)),
                                  borderSide: BorderSide(
                                      color: Colors.white24, width: 0.5)),
                              prefixIcon: const Icon(
                                Icons.email,
                                color: Colors.white,
                              ),
                            ),
                            keyboardType: TextInputType.emailAddress,
                            validator: (text) {
                              if (text.isEmpty || !text.contains("@"))
                                return "E-mail inválido!";
                            },
                          ),
                          SizedBox(
                            height: 10.0,
                          ),
                          TextFormField(
                            style: TextStyle(color: Colors.white),
                            controller: _passController,
                            decoration: InputDecoration(
                              hintStyle: TextStyle(
                                  color: Colors.white,
                                  fontFamily: "WorkSansLight",
                                  fontSize: 15.0),
                              filled: true,
                              fillColor: Colors.white24,
                              hintText: "Senha",
                              border: OutlineInputBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(90.0)),
                                  borderSide: BorderSide(
                                      color: Colors.white24, width: 0.5)),
                              prefixIcon: const Icon(
                                Icons.lock_outline,
                                color: Colors.white,
                              ),
                            ),
                            obscureText: true,
                            validator: (text) {
                              if (text.isEmpty || text.length < 6)
                                return "Senha inválida!";

                            },
                          ),
                          Align(
                            alignment: Alignment.centerRight,
                            child: FlatButton(
                              onPressed: () {
                                if (_emailController.text.isEmpty)
                                  _scaffoldKey.currentState
                                      .showSnackBar(SnackBar(
                                    content: Text(
                                        "Insira seu e-mail para recuperação!"),
                                    backgroundColor: Colors.redAccent,
                                    duration: Duration(seconds: 2),
                                  ));
                                else {
                                  model.recoverPass(_emailController.text);
                                  _scaffoldKey.currentState
                                      .showSnackBar(SnackBar(
                                    content: Text("Confira seu e-mail!"),
                                    backgroundColor: Colors.purple,
                                    duration: Duration(seconds: 3),
                                  ));
                                }
                              },
                              child: Text(
                                "Esqueci minha senha",
                                textAlign: TextAlign.right,
                                style: TextStyle(
                                    color: Colors.white, fontSize: 13.0),
                              ),
                              padding: EdgeInsets.zero,
                            ),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          SizedBox(
                            height: 75,
                            width: 75,
                            child: new FloatingActionButton(
                              backgroundColor: Colors.white30,
                              child: Text(
                                "Entrar",
                                style: TextStyle(
                                  fontSize: 18.0,
                                ),
                              ),
                              onPressed: () {
                                if (_formKey.currentState.validate()) {}
                                model.signIn(
                                    email: _emailController.text,
                                    pass: _passController.text,
                                    onSuccess: _onSuccess,
                                    onFail: _onFail);
                              },
                            ),
                          ),
                          SignUpButton()
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  void _onSuccess() {
    Navigator.of(context).pop();
  }

  void _onFail() {
    _scaffoldKey.currentState.showSnackBar(SnackBar(
      content: Text("Falha ao Entrar!"),
      backgroundColor: Colors.redAccent,
      duration: Duration(seconds: 3),
    ));
  }
}

I/flutter ( 5343): The following ScopedModelError was thrown building ScopedModelDescendant(dirty):
I/flutter ( 5343): Error: Could not find the correct ScopedModel.
I/flutter ( 5343):
I/flutter ( 5343): To fix, please:
I/flutter ( 5343):
I/flutter ( 5343): * Provide types to ScopedModel
I/flutter ( 5343): * Provide types to ScopedModelDescendant
I/flutter ( 5343): * Provide types to ScopedModel.of()
I/flutter ( 5343): * Always use package imports. Ex: `import 'package:my_app/my_model.dart';
I/flutter ( 5343):

In your main, you should init the model, otherwise there is no model of course.. e.g:

@override
  Widget build(BuildContext context) {
    final MainModel model = MainModel();

    return ScopedModel<MainModel>(
      model: model,
      child: MaterialApp(
        title: "De Kleine Gast",
        navigatorObservers: [
          CupertinoSwipeBackObserver(),
        ],
        debugShowCheckedModeBanner: false,
        debugShowMaterialGrid: false,
        routes: {
          '/': (BuildContext context) => NewLoginScreen(model),

        },
      ),

    );
  }

After that you can pass your model around. It is called Descendant for a reason :)