【问题标题】:Failed assertion: boolean expression must not be null in Flutter断言失败:布尔表达式在 Flutter 中不能为空
【发布时间】:2021-01-18 12:27:51
【问题描述】:

我正在尝试从我的 onPressed() 函数中名为“PostAd”的另一个屏幕导航到我的“MainScreen”。但我收到布尔值不能为空的错误。当我单击错误时,它会将我带到“/mainScreen”这一行上的“HomePage”类:(context) => MainScreen()。很久以来我一直在尝试解决这个问题。任何帮助,将不胜感激。谢谢

  Class PostAd{
    @override
      Widget build(BuildContext context) {
    
        return Scaffold(
    //      backgroundColor: Colors.grey[600],
        key: _scaffoldKey,
          body: Container(
    //        decoration: BoxDecoration(
    //          image: DecorationImage(
    //            image: AssetImage("assets/background.png"),
    //            fit: BoxFit.cover,
    //          ),
    //        ),
            padding: EdgeInsets.all(16.0),
            child: Form(
              child: ListView(
                shrinkWrap: true,
                children: <Widget>[
                  Center(
                    child: Text(
                      "Post New Ad",
                      style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
                    ),
                  ),
                  AddPost(),
    
                  Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 16),
                  ),
                ], //:TODO: implement upload pictures
              ),
            ),
          ),
        );
    
    
    
      }
    
      Widget AddPost(){
        return Form(
          key: _key,
          autovalidate: _validate,
          child: Padding(
              padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
              child:Column(
                children: <Widget>[
    
                  _getPropertyTypeDropDown(),
                  _getPropertyTypeDetailDropDown(),
    
                  _selectedpropertyDetailType=='House'? _showExpansionList(): _selectedpropertyDetailType=='Flat'? _showExpansionList():_selectedpropertyDetailType=='Upper Portion'? _showExpansionList():_selectedpropertyDetailType=='Lower Portion'? _showExpansionList():_selectedpropertyDetailType=='Farm House'? _showExpansionList(): _selectedpropertyDetailType=='Residential Plot'? _showExpansionListPlot():_selectedpropertyDetailType=='Commercial Plot'? _showExpansionListPlot():_selectedpropertyDetailType=='Agricultural Plot'? _showExpansionListPlot(): _selectedpropertyDetailType=='Office'? _showExpansionListCommercial(): _selectedpropertyDetailType=='Shop'? _showExpansionListCommercial(): _selectedpropertyDetailType=='Ware House'? _showExpansionListCommercial(): _selectedpropertyDetailType=='Factory'? _showExpansionListCommercial():_selectedpropertyDetailType=='Building'? _showExpansionListCommercial(): _selectedpropertyDetailType=='Other'? _showExpansionListCommercial():_hideExpansionList(),
    
    
    
                    UploadPropertyImages(),
    
                  //ReadImagesFromFirebaseStorage(),
                  //_uploadImagesInput(),
                  //_showSelectImages(),
                  //_showAddImages(),
                  Container(
                    margin: EdgeInsets.only(left: 7),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                    //buildGridView(),
                        Container(
                          width: 200,
                          height: 200,
                            //color: Colors.green,
                            child: buildGridView(),
                        ),
                        RaisedButton(
                          child: Text("Submit"),
                          onPressed: () {
                            if(_key.currentState.validate()){
                              _key.currentState.save();
                              showAlert("Post is Uploading. Please Wait ");
                              runMyFutureGetImagesReference();
    
                              Navigator.pushNamed(context, '/mainScreen');
    
                            }
    
                            else{
                              setState(() {
                                _validate = true;
                              });
                            }
    
                          //  Navigator.pushNamed(context, '/mainScreen');
    //                      setState(() {
    //                        isButtonPressed7 = !isButtonPressed7;
    //                      });
                          },
                          textColor: Colors.black,
                          padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                          splashColor: Colors.grey,
                        ),
                      ],
                    ),
                  ),

 Class MainScreen{
class MainScreen extends StatefulWidget {
  final bool isAdmin;

  const MainScreen({Key key, this.isAdmin}) : super(key: key);

  @override
  _MainScreenState createState() => _MainScreenState(this.isAdmin);
}

class _MainScreenState extends State<MainScreen> {
  bool isPressed = false;
  bool userLoggedIn =false;
  //var _userName;
//  Future<FirebaseUser> getUser() {
//    return _auth.currentUser();
//  }
  Future<String> currentUser() async {
   user = await _auth.currentUser();
    return user != null ? user.uid : null;
  }
  FirebaseUser user;
  @override
  void initState() {
    super.initState();

    initUser();

  }
  String userid;

  initUser() async {
    user = await _auth.currentUser();
    if (user != null) {
      userid = user.uid;
    } else {
      print("user.uid");
      // User is not available. Do something else
    }
    setState(() {});
  }

  bool isAdmin = true;
  final FirebaseAuth _auth = FirebaseAuth.instance;
  //final authData = snapshot.data;
  _MainScreenState(this.isAdmin);

}
Class HomePage{
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => CurrentUser(),
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        home: MainScreen(isAdmin: false,),
        routes: {
          '/main': (context) => HomePage(),
          '/home': (context) => RoleCheck(),
          ActivityFeed.routeName: (context) => ActivityFeed(),
          //'/myProfileFinal': (context) => MyProfileFinal(),
          MyProfileFinal.routeName: (ctx) => MyProfileFinal(),
          ImageCarousel.routeName: (ctx) => ImageCarousel(),
         // Random.routeName: (ctx) => Random(),
          '/UserProfile': (context) => UserProfile(),
          '/mainScreen': (context) => MainScreen(),
          '/mainScreenUser': (context) => MainScreenUsers(),
          '/LoginScreen': (context) => LoginPage(),
          '/MyProfile': (context) => MyProfile(),
          '/Signup': (context) => SignUpPage(),
          '/ViewAdds': (context) => ViewAdds(),
            '/PostDetail': (context) =>PostDetail(),
          '/postscreen1': (context) => PostFirstScreen(),
          '/choseOnMap':(context) => ChoseOnMap(),
          '/AgentSignup': (context) => AgentSignUp(),
          '/ForgetPassword': (context) => ForgetPassword(),
          '/ImageCarousel': (context) => ImageCarousel(),
          '/AgentSignup': (context) => AgentSignUp(),
          '/ForgetPassword': (context) => ForgetPassword(),

        },
      ),
    );
  }
}

【问题讨论】:

  • 您希望isAdmin 具有什么价值?如果你导航到/mainScreen,它目前为空。
  • 您应该发送 isAdmin 参数,因为它为空。或使用条件检查 isAdmin 是否为空。

标签: flutter


【解决方案1】:

你需要像下面这样传递布尔值。

RaisedButton(
    child: Text("Submit"),
    onPressed: () {
      Navigator.pushNamed(
        context,
        '/mainScreen',
        arguments: {
          "isAdmin": isAdmin,
        },
      );
    },
    textColor: Colors.black,
    padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
    splashColor: Colors.grey,
  )

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2020-04-23
    • 2020-07-05
    相关资源
    最近更新 更多