无法将参数类型'Widget Function(BuildContext)'分配给参数类型'Widget Function(BuildContext,Widget)'

时间:2020-05-14 11:40:10

标签: android-studio flutter provider flutter-change-notifier

这是一个代码:

import 'package:flutter/material.dart';
import 'package:flutterapp/ui/pages/notes_home.dart';
import 'package:provider/provider.dart';
import 'package:flutterapp/ui/pages/splash.dart';
import 'package:flutterapp/ui/pages/user_info.dart';
import 'package:flutterapp/ui/pages/auth/login.dart';
import 'package:flutterapp/model/user_repository.dart';
import 'package:path/path.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      builder: (_) => UserRepository.instance(),
      child: Consumer
        // ignore: missing_return
        (builder: (context, UserRepository user, _) {
          // ignore: missing_return
          switch (user.status){
            case Status.Uninitialized:
              return Splash();
            case Status.Unauthenticated:
            case Status.Authenticating:
              return LoginPage();
            case Status.Authenticated:
              return NotesHomePage();
          }
      }),
    );
  }
}

以上代码中的“ builder:(_)=> UserRepository.instance()”行显示错误

不能将参数类型'Widget Function(BuildContext)'分配给参数类型'Widget Function(BuildContext,Widget)'

每当我运行程序时,它就会显示

在此LoginPage小部件上方找不到正确的提供程序

UserRepository代码:

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

enum Status { Uninitialized, Authenticated, Authenticating, Unauthenticated }

class UserRepository with ChangeNotifier {
  FirebaseAuth _auth;
  FirebaseUser _user;
  GoogleSignIn _googleSignIn;
  Status _status = Status.Uninitialized;

  UserRepository.instance()
      : _auth = FirebaseAuth.instance,
        _googleSignIn = GoogleSignIn() {
      _auth.onAuthStateChanged.listen(_onAuthStateChanged);
  }

  Status get status => _status;
  FirebaseUser get user => _user;

  Future<bool> signIn(String email, String password) async{
    try {
      _status = Status.Authenticating;
      notifyListeners();
      await _auth.signInWithEmailAndPassword(email: email, password: password);
      return true;
    }catch(e){
      _status = Status.Unauthenticated;
      notifyListeners();
      return false;
    }
  }

  Future<bool> signUp(String email, String password) async{
    try {
      _status = Status.Authenticating;
      notifyListeners();
      await _auth.createUserWithEmailAndPassword(email: email, password: password);
      return true;
    }catch(e){
      _status = Status.Unauthenticated;
      notifyListeners();
      return false;
    }
  }

  Future<bool> signInWithGoogle() async{
    try{
      _status =Status.Authenticating;
      notifyListeners();
      final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
      final GoogleSignInAuthentication googleAuth =
          await googleUser.authentication;
      final AuthCredential credential = GoogleAuthProvider.getCredential(idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
      await _auth.signInWithCredential(credential);
      return true;
    }catch(e) {
      print(e);
      _status = Status.Unauthenticated;
      notifyListeners();
      return false;
    }
  }

  Future signOut() async {
    _auth.signOut();
    _googleSignIn.signOut();
    _status = Status.Unauthenticated;
    notifyListeners();
    return Future.delayed(Duration.zero);
  }

  Future<void> _onAuthStateChanged(FirebaseUser firebaseUser) async{
    if(firebaseUser == null) {
      _status = Status.Unauthenticated;
    }else{
      _user = firebaseUser;
      _status = Status.Authenticated;
    }
    notifyListeners();
  }
}

请帮助

enter image description here

这是登录页代码:

import 'package:flutter/material.dart';
import 'package:flutterapp/model/user_repository.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
  bool signInForm;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    signInForm = true;
  }

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<UserRepository>(context);
    return WillPopScope(
      onWillPop: () async{
        if(!signInForm) {
          setState(() {
            signInForm = true;
          });
          return false;
        }else{
          return true;
        }
      },
      child: Scaffold(
        key: _key,
        backgroundColor: Colors.red,
        body: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: SizedBox(
            width: double.infinity,
            child: Column(
              children: <Widget>[
                const SizedBox(height: kToolbarHeight),
                Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    shape: BoxShape.circle,
                  ),
                  width: 60.0,
                  height: 60.0,
                ),
                const SizedBox(height: 30.0),
                RaisedButton(
                  textColor: Colors.red,
                  color: Colors.white,
                  child: Text('Google'),
                  onPressed: () async{
                    if(!await user.signInWithGoogle())
                      showmessage();
                  },
                ),
                const SizedBox(height: 30.0),
                AnimatedSwitcher(
                  child: signInForm ? LoginForm() : SignupForm(),
                  duration: Duration(milliseconds: 200),
                ),
                const SizedBox(height: 20.0,),
                OutlineButton(
                  textColor: Colors.white,
                  child: signInForm ? Text("Sign Up", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0),) : Icon(Icons.arrow_back),
                  onPressed: () {
                    setState(() {
                      signInForm = !signInForm;
                    });
                  },
                  color: Colors.white,
                  borderSide: BorderSide(color: Colors.white),
                  highlightColor: Colors.white,
                )
              ],
            ),
          )
        ),
      ),
    );
  }
  void showmessage(){
    _key.currentState.showSnackBar(SnackBar(
      content: Text("Somethimg is wrong"),
    ));
  }
}

class LoginForm extends StatefulWidget {
  final Function showError;

  const LoginForm({Key key, this.showError}) : super(key: key);
  @override
  _LoginFormState createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final FocusNode passwordField = FocusNode();
  TextEditingController _email;
  TextEditingController _password;

  @override
  void initState() {
    _email = TextEditingController();
    _password = TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<UserRepository>(context);
    return Container(
      padding: const EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.0),
      ),
      child: Form(
        key: _formKey,
        child: Column(
          children: <Widget>[
            Text("Login", style: Theme.of(context).textTheme.display1,),
            const SizedBox(height: 20.0),
            TextFormField(
              controller: _email,
              textInputAction: TextInputAction.next,
              decoration: InputDecoration(
                  labelText: "Email"
              ),
              onEditingComplete: (){
                FocusScope.of(context).requestFocus(passwordField);
              },
            ),
            const SizedBox(height: 16.0,),
            TextFormField(
              controller: _password,
              focusNode: passwordField,
              obscureText: true,
              decoration: InputDecoration(
                  labelText: "Password"
              ),
            ),
            const SizedBox(height: 20.0),
            RaisedButton(
              textColor: Colors.red,
              child: Text("Login"),
              onPressed: () async{
                if(_formKey.currentState.validate()){
                  if(!await user.signIn(
                    _email.text, _password.text))
                    widget.showError();
                }
              },
            )
          ],
        ),
      ),
    );
  }
}

class SignupForm extends StatefulWidget {
  @override
  _SignupFormState createState() => _SignupFormState();
}

class _SignupFormState extends State<SignupForm> {
  final FocusNode passwordField = FocusNode();
  final FocusNode confirmPasswordField = FocusNode();
  TextEditingController _email;
  TextEditingController _password;
  TextEditingController _confirmpassword;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  void initState() {
    _email = TextEditingController();
    _password = TextEditingController();
    _confirmpassword = TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<UserRepository>(context);
    return Container(
      padding: const EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.0),
      ),
      child: Form(
        key: _formKey,
        child: Column(
          children: <Widget>[
            Text("Sign Up", style: Theme.of(context).textTheme.display1,),
            const SizedBox(height: 20.0),
            TextFormField(
              controller: _email,
              textInputAction: TextInputAction.next,
              decoration: InputDecoration(
                  labelText: "Email"
              ),
              onEditingComplete: (){
                FocusScope.of(context).requestFocus(passwordField);
              },
            ),
            const SizedBox(height: 16.0,),
            TextFormField(
              obscureText: true,
              controller: _password,
              focusNode: passwordField,
              decoration: InputDecoration(
                  labelText: "Password"
              ),
              onEditingComplete: ()=> FocusScope.of(context).requestFocus(confirmPasswordField),
            ),
            const SizedBox(height: 16.0,),
            TextFormField(
              obscureText: true,
              controller: _confirmpassword,
              focusNode: confirmPasswordField,
              decoration: InputDecoration(
                  labelText: "Confirm Password"
              ),
              onEditingComplete: (){
                FocusScope.of(context).requestFocus(passwordField);
              },
            ),
            const SizedBox(height: 20.0),
            RaisedButton(
              textColor: Colors.red,
              child: Text("Create Account"),
              onPressed: () async{
                if(_formKey.currentState.validate()){
                  if(_confirmpassword.text == _password.text)
                    if(!await user.signUp(
                      _email.text, _password.text))
                      print("Failed to Signup");
                }
              },
            )
          ],
        ),
      ),
    );
  }
}

6 个答案:

答案 0 :(得分:2)

我对颤振很陌生,我遇到了同样的问题,然后我用这个解决方案解决了..

<块引用>

我用 builder 替换了 create,如图所示。

enter image description here

答案 1 :(得分:2)

对于 provider 3.2 及更高版本,您应该使用 create 而不是 builder

答案 2 :(得分:1)

在您的代码中,只需更改

builder: (_) => UserRepository.instance(),

create: (_) => UserRepository.instance(),

一切都会正常!

答案 3 :(得分:0)

尝试一下:

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserRepository>(
      create: (context) => UserRepository.instance(),
      child: Consumer
        // ignore: missing_return
        (builder: (context, UserRepository user, child) {
          // ignore: missing_return
          switch (user.status){
            case Status.Uninitialized:
              return Splash();
            case Status.Unauthenticated:
            case Status.Authenticating:
              return LoginPage();
            case Status.Authenticated:
              return NotesHomePage();
          }
      }),
    );
  }
}

答案 4 :(得分:0)

使用 create:代替 builder:在 ChangeNotifiereProvider() 中

答案 5 :(得分:0)

从 provider 包的 5.0.0 版本开始,我们应该在 ChangeNotifierProvider 中使用 'create' 而不是 'builder' 。 >