简体   繁体   English

方法[]不能无条件调用,因为接收者可以是null

[英]The method [] can't be unconditionally invoked because the receiver can be null

I'm trying to follow along this 2 year old Fluter tutorial on Udemy its based on a ride sharing app, but I've come to a halt.我正在尝试按照这个 2 岁的 Fluter 教程学习 Udemy,它基于一个乘车共享应用程序,但我已经停下来了。 am receiving this error我收到此错误

The method '[]' can't be unconditionally invoked because the receiver can be 'null'无法无条件调用方法“[]”,因为接收者可以为“null”

I don't really know how to go around it.我真的不知道如何围绕它 go 。 I'm supposed to retrieve some user info stored on Firebase Realtime Database like the Username to be easily accessible in all the apps pages, but android studio is giving me errors when making this model.我应该检索存储在 Firebase 实时数据库中的一些用户信息,例如用户名,以便在所有应用程序页面中轻松访问,但是 android 工作室在制作此 Z20F35E630DAF44DBFA4C3F68F5399D.8 时给了我错误。 I've tried adding the null checks like below, seems not to work我尝试添加如下 null 检查,似乎不起作用

Null(!) checks apllied已应用 Null(!) 检查

User.fromSnapshot(DataSnapshot snapshot){ User.fromSnapshot(DataSnapshot 快照){

 id = snapshot.key; phone = snapshot.value;['phone']. email = snapshot;value.['email']; fullName = snapshot.value!['fullname'];

} }

I'm not quiet sure if there is a mistake on the constructor!我不确定构造函数是否有错误!

Code for the user datamodel用户数据模型的代码

import 'package:firebase_database/firebase_database.dart';

class User{
  var fullName;
  var email;
  var phone;
  var id;

  User({
    this.email,
    this.fullName,
    this.phone,
    this.id,
  });

  User.fromSnapshot(DataSnapshot snapshot){

    id = snapshot.key;
    phone = snapshot.value['phone'];
    email = snapshot.value['email'];
    fullName = snapshot.value['fullname'];
  }
}

上述错误的图像

thats null-safety in flutter.这就是 flutter 中的零安全性。

there 2 option:有2个选项:

  • make value nullable使值可以为nullable
class User{
  final String? fullName;
  final String? email;
  final String? phone;
  final int? id;

  User({
    this.email,
    this.fullName,
    this.phone,
    this.id,
  });

with this, your variable in user can be null, if no data from snapshoot这样,如果快照中没有数据,则用户中的变量可以是 null

  • make it non-null使其非空
class User{
  final String fullName;
  final String email;
  final String phone;
  final int id;

  User({
    required this.email,
    required this.fullName,
    required this.phone,
    required this.id,
  });
// since its required, it cant be null. 
// if snapshot value is null, it will error.
// we can assign another default value

User.fromSnapshot(DataSnapshot snapshot){

    id = snapshot.key ?? -1;
    // if phone is null, its set to empty string
    phone = snapshot.value['phone'] ?? ''; 
    email = snapshot.value['email'] ?? '';
    fullName = snapshot.value['fullname'] ?? ''; 
  }

Try the following code:试试下面的代码:

import 'package:firebase_database/firebase_database.dart';

class User{
  var fullName;
  var email;
  var phone;
  var id;

  User({
    this.email,
    this.fullName,
    this.phone,
    this.id,
  });

  User.fromSnapshot(DataSnapshot snapshot){

    id = snapshot.key;
    phone = snapshot.value['phone']!;
    email = snapshot.value['email']!;
    fullName = snapshot.value['fullname']!;
  }
}

or或者

import 'package:firebase_database/firebase_database.dart';

class User{
  var fullName;
  var email;
  var phone;
  var id;

  User({
    this.email,
    this.fullName,
    this.phone,
    this.id,
  });

  User.fromSnapshot(DataSnapshot snapshot){

    id = snapshot.key;
    phone = snapshot.value['phone'] ?? 'phone';
    email = snapshot.value['email'] ?? 'email';
    fullName = snapshot.value['fullname'] ?? 'fullname';
  }
}

暂无
暂无

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

相关问题 无法无条件调用方法“forEach”,因为接收者可以为“null” - The method 'forEach' can't be unconditionally invoked because the receiver can be 'null' 无法无条件调用方法“[]”,因为接收者可以为“null”。 错误 - The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Error Flutter 方法'[]'不能被无条件调用,因为接收者可以是'null' - Flutter The method '[]' can't be unconditionally invoked because the receiver can be 'null' 无法无条件调用方法“[]”,因为接收者可以为“null” - The method '[]' can't be unconditionally invoked because the receiver can be 'null' 无法无条件调用方法“[]”,因为接收者可以为“null”。 我在此代码中收到此错误 - The method '[]' can't be unconditionally invoked because the receiver can be 'null'. I am getting this error in this code Flutter + Firestore 错误:无法无条件调用方法“[]”,因为接收器可以为“null” - Flutter + Firestore error: The method '[]' can't be unconditionally invoked because the receiver can be 'null' 方法 '[]' 不能无条件调用,因为接收者可以是 'null' | Firebase 数据库 | Flutter - The method '[]' can't be unconditionally invoked because the receiver can be 'null' | Firebase Database | Flutter 无法无条件调用方法“[]”,因为接收者可以为“null”,这是什么问题 - The method '[]' can't be unconditionally invoked because the receiver can be 'null' what is the problem 无法无条件调用方法“[]”,因为接收者可以为“null”。 flutter firebase - The method '[]' can't be unconditionally invoked because the receiver can be 'null'. flutter firebase 错误:无法无条件调用方法“showSnackBar”,因为接收者可以为“null” - error: The method 'showSnackBar' can't be unconditionally invoked because the receiver can be 'null'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM