简体   繁体   English

Flutter 错误:Null 检查运算符用于 StreamBuilder 中的 null 值

[英]Flutter Error: Null check operator used on a null value in a StreamBuilder

Flutter Error: Null check operator used on a null value in a StreamBuilder Flutter 错误:Null 检查运算符用于 StreamBuilder 中的 null 值

no matter what i do i keep getting this error instead of the user data that i want, in the database.dart i didn't put any?无论我做什么,我都会在数据库中不断收到此错误而不是我想要的用户数据。dart 我没有放任何东西? or, so i guess the problem's from this file.或者,所以我猜问题出在这个文件上。 but i have no clue how to fix it.但我不知道如何解决它。 i also tried all the commands i found on google but none of them works我也尝试了我在谷歌上找到的所有命令,但它们都不起作用


class SettingsForm extends StatefulWidget {
  const SettingsForm({Key? key}) : super(key: key);

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

class _SettingsFormState extends State<SettingsForm> {

  final _formKey = GlobalKey<FormState>();
  final List<String> sugars = ['0','1','2','3','4'];

  String?_currentName ;
  String?_currentSugars ;
  dynamic _currentStrength =1;

  @override
  Widget build(BuildContext context) {


    final user = Provider.of<myUser?>(context);


    return StreamBuilder<myUserData?>(
      stream: DatabaseService(uid: user!.uid).userData,

      builder: (context, snapshot) {
        if(snapshot.hasData) {
          myUserData? usdata = snapshot.data;
          return Form(
            key:_formKey,
            child: Column(
              children: [
                Text('Update your brew settings.',
                  style: TextStyle(fontSize:18.0),),
                SizedBox(height: 20,),
                TextFormField(
                  initialValue: usdata?.name,
                  decoration: textInputDecoration.copyWith(hintText: ' name'),
                  validator: (val) => val!.isEmpty ? 'Please enter a name' : null,
                  onChanged: (val) {
                    setState(() => _currentName = val);
                  },
                ),
                SizedBox(height: 20.0,),
                //dropdown
                DropdownButtonFormField<String>(
                  value: usdata?.sugars,


                  items: sugars.map((sugar){
                    return DropdownMenuItem(
                        value: sugar,
                        child: Text(' $sugar sugars')
                    );
                  }).toList(),
                  onChanged: (val) => setState(() => _currentSugars = val.toString()),
                ),
                SizedBox(height:20 ),
                //slider
                Slider(
                  value: (_currentStrength ?? usdata?.strength).toDouble(),
                  activeColor: Colors.brown[_currentStrength ?? usdata?.strength],
                  inactiveColor: Colors.brown[_currentStrength ?? usdata?.strength],
                  min:100,
                  max:900,
                  divisions: 8,
                  onChanged: (val) => setState(() {
                    _currentStrength = val.round();
                  }),
                ),
                RaisedButton(
                    color:Colors.pink[400],
                    child: Text('Update',
                      style: TextStyle(color:Colors.white),),
                    onPressed: () async {
                      print(_currentName);
                      print(_currentSugars);
                      print(_currentStrength);
                    })

              ],

            ),
          );
        }
else {
          myUserData usdata = snapshot.data!;
          print(usdata.name);
          print(usdata.sugars);
          print(usdata.strength);
          return Container();
        }
      }
    );
  }
}

Here is a simplification of your code:这是您的代码的简化:

builder: (context, snapshot) {
  if(snapshot.hasData) {
    ...
  } else {
    myUserData usdata = snapshot.data!;
    print(usdata.name);
    print(usdata.sugars);
    print(usdata.strength);
    return Container();
  }
},

The code in your else statement will only run if snapshot.hasData is false, which means that snapshot.data is null, giving you an error when you try to read it. else语句中的代码只有在snapshot.hasData为 false 时才会运行,这意味着snapshot.data是 null,当您尝试读取它时会出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM