【问题标题】:Null check used on null value对空值使用空检查
【发布时间】:2022-07-13 22:30:27
【问题描述】:

当我尝试将用户信息写入 firestore 时,我不断收到“用于空值的空检查运算符”。我该如何解决这个错误?

这是我尝试实现的代码的 sn-p-

    Widget build(BuildContext context) {
    final UserProvider userProvider = Provider.of<UserProvider>(context);
    return Scaffold(
      appBar: AppBar(
        elevation: 1.0,
        iconTheme: const IconThemeData(
          color: Colors.black,
        ),
        title: const Text(
          "Inventory",
          style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
        ),
        backgroundColor: Colors.white,
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 60.0, horizontal: 16.0),
          child: Column(
            children: <Widget>[
              GestureDetector(
                onTap: () => _selectImage(context),
                child: Container(
                  margin: const EdgeInsets.only(),
                  width: MediaQuery.of(context).size.width,
                  height: 210.0,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: _file == null
                          ? const NetworkImage(
                                  'https://i.stack.imgur.com/l60Hf.png')
                              as ImageProvider
                          : MemoryImage(_file!),
                    ),
                  ),
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              Container(
                padding: const EdgeInsets.only(top: 10),
                width: MediaQuery.of(context).size.width,
                margin: const EdgeInsets.symmetric(horizontal: 20),
                child: TextFieldInput(
                  maxLines: 1,
                  maxLength: TextField.noMaxLength,
                  controller: _mealNameController,
                  labelText: 'Meal Name',
                ),
              ),
              Container(
                padding: const EdgeInsets.only(top: 10),
                width: MediaQuery.of(context).size.width,
                margin: const EdgeInsets.symmetric(horizontal: 20),
                child: TextFieldInput(
                  maxLines: 1,
                  maxLength: TextField.noMaxLength,
                  controller: _priceController,
                  labelText: 'price',
                ),
              ),
              Container(
                padding: const EdgeInsets.only(top: 10),
                width: MediaQuery.of(context).size.width,
                margin: const EdgeInsets.symmetric(horizontal: 20),
                child: TextFieldInput(
                  maxLines: 5,
                  controller: _mealDescriptionController,
                  labelText: 'Meal description',
                  maxLength: 150,
                ),
              ),
              const SizedBox(height: 28.0),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  mealSize(context),
                  const SizedBox(height: 9.0),
                  mealSize(context),
                  const SizedBox(height: 9.0),
                  mealSize(context),
                ],
              ),
              const SizedBox(height: 40.0),
              GestureDetector(
                  onTap: () => addMenu(userProvider.getUser.name),
                  child: const Button(btnText: "Add Food Item")),
            ],
          ),
        ),
      ),
    );
  }

userprovider.dart:

    class UserProvider with ChangeNotifier {
  User? _user;

  final AuthController _authMethods = AuthController();

  User get getUser => _user!;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

这是我调用函数时遇到的错误:

══════ Exception caught by gesture ═══════════════════════════════════════════
The following _CastError was thrown while handling a gesture:
Null check operator used on a null value

When the exception was thrown, this was the stack
#0      `UserProvider.getUse`r
package:webite/provider/user_provider.dart:11
#1      _AddMealState.build.<anonymous closure>

【问题讨论】:

    标签: flutter dart flutter-provider dart-null-safety


    【解决方案1】:

    您是否尝试过查看这篇文章的答案?长得和我莫名的相似。 Null check operator used on a null value

    尤其是在两个地点:

    
      User get getUser => _user!; <---- right here
    
                            image: _file == null
                              ? const NetworkImage(
                                      'https://i.stack.imgur.com/l60Hf.png')
                                  as ImageProvider
                              : MemoryImage(_file!), <--- and here
        ```
    
    

    【讨论】:

    • 所以如果检查 _user == null 我应该或多或少?
    • 听起来是个好主意,从技术上讲,您不应该让用户在 Firestore 为空时向其写入信息
    猜你喜欢
    • 2021-08-04
    • 2021-08-10
    • 2021-11-10
    • 2021-08-13
    • 2021-08-11
    • 2021-09-25
    • 1970-01-01
    • 2021-06-15
    • 2020-07-22
    相关资源
    最近更新 更多