断言失败:Flutter中的布尔表达式不能为null

时间:2020-10-02 20:52:27

标签: flutter

我正在尝试从onPressed()函数中另一个名为“ PostAd”的屏幕导航至“ MainScreen”。但是我收到错误消息,布尔值不能为null。当我单击错误时,将我带到“ / mainScreen”这一行上的“首页”类:(上下文)=> 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(),

        },
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

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

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,
  )