【问题标题】:I have 1 error popping up but When I try to run the app it says I have all of these other errors that are wrong我弹出 1 个错误,但是当我尝试运行该应用程序时,它说我有所有其他错误
【发布时间】:2022-01-18 05:32:30
【问题描述】:

enter image description here

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:time_trackerpractice/app/sign_in/home_page.dart';
import 'package:time_trackerpractice/app/sign_in/sign_in_page.dart';
import 'package:time_trackerpractice/services/auth.dart';

class LandingPage extends StatefulWidget {
  const LandingPage({Key key, this.auth}) : super(key: key);
  final AuthBase auth;

  @override
  _LandingPageState createState() => _LandingPageState();
}


class _LandingPageState extends State<LandingPage> {

  User _user;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _updateUser(widget.auth.currentUser);
  }


  void _updateUser(User user) {
    setState(() {
      _user = user;
    });
  }

  

  @override
  Widget build(BuildContext context) {
   return StreamBuilder<User>(
     stream: widget.auth.authStateChanges(),
     builder: (context,snapshot){
     if(snapshot.connectionState == ConnectionState.active){
       final User user =snapshot.data;
       if(user == null){
         return SignInPage(
           auth: widget.auth,
           onSignIn: _updateUser,
         );
       }
       return HomePage(
           auth: widget.auth,
           onSignOut: => _updateUser(null),
       );
     }

       return Scaffold(
       body:Center(
       child: CircularProgressIndicator(),
       );
       );

     },
   );

我一直在学习颤振课程,并且在我为应用程序键入的代码上弹出 1 个错误,但是当我尝试运行应用程序时,它说我有所有其他错误的错误。这个是唯一出现的错误和分号onSignOut: =&gt; _updateUser(null),所以我不知道我错了。但这些是下面弹出的错误:

lib/app/sign_in/landing_page.dart:38:38: Error: Can't find '}' to match '{'.
Widget build(BuildContext context) {
                                   ^
lib/app/sign_in/landing_page.dart:16:52: Error: Can't find '}' to match '{'.
class _LandingPageState extends State<LandingPage> {
                                                   ^
lib/app/sign_in/landing_page.dart:52:23: Error: Expected an identifier, but got '=>'.
Try inserting an identifier before '=>'.
       onSignOut: => _updateUser(null),
                  ^^
lib/app/sign_in/landing_page.dart:52:26: Error: Expected ',' before this.
       onSignOut: => _updateUser(null),
                     ^^^^^^^^^^^
lib/app/sign_in/landing_page.dart:52:26: Error: Place positional arguments before named arguments.
Try moving the positional argument before the named arguments, or add a name to the argument.
       onSignOut: => _updateUser(null),
                     ^^^^^^^^^^^
lib/app/sign_in/landing_page.dart:52:26: Error: Expected named argument.
           onSignOut: => _updateUser(null),
                         ^
lib/app/sign_in/landing_page.dart:50:23: Error: No named parameter with the name '#2'.

【问题讨论】:

    标签: flutter android-studio


    【解决方案1】:

    您有两个错误,一个是您没有关闭方法,请在代码末尾添加此行:

    }}
    

    应该是这样的:

     ...
     return Scaffold(
           body:Center(
           child: CircularProgressIndicator(),
           );
           );
    
         },
       );
     }}
    

    之所以需要这样做是因为您必须同时关闭构建和小部件类

    class _LandingPageState extends State<LandingPage> {
    
      @override
      Widget build(BuildContext context) {
        // some code
      } // <--- you missed this
    } // <--- you also missed this one, so you must add two }} at the end of the file
    

    第二个错误,你忘记了lambda函数上的括号:

    从这里:

    onSignOut: => _updateUser(null),
    

    到这里:

    onSignOut: () => _updateUser(null),
    

    如果您不了解 lambda 是什么,请查看以下资源:https://dart.dev/guides/language/language-tour#anonymous-functions

    【讨论】:

    • 感谢您的帮助。我真的很感激。它帮助了我很多
    猜你喜欢
    • 2022-12-21
    • 2020-08-04
    • 2020-02-20
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2019-09-23
    相关资源
    最近更新 更多