【问题标题】:Data Not saving in real time database flutter数据未实时保存数据库颤动
【发布时间】:2021-04-16 17:45:36
【问题描述】:

我有一个应用程序,我需要在实时数据库中保存数据,我有用于身份验证、存储和实时数据库的设置代码,身份验证和存储工作正常,但不是后者, 我尝试了不同的方法来使实时数据库工作,但不幸的是没有成功,任何帮助将不胜感激,谢谢。

  • 这就是我初始化 firebase 的方式
 FirebaseApp app;
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  app = await Firebase.initializeApp(
      name: 'SecondaryApp',
      options: const FirebaseOptions(
          appId: '1:729669525962:android:b878xxxxxxxxxxxxx',
          apiKey: 'AIzaSyD0w6UnBrWxxxxxxxxxxxxxxxxxxxxxx',
          databaseURL: 'https://xxxxxxxxxxxx.appspot.com',
          messagingSenderId: 'xxxxxxxxxxxxxxxx',
          projectId: 'groceryfxxxxxxxxf6'
      )
  );
  runApp(MaterialApp(
    home: UserRegistrationScreen(),
    debugShowCheckedModeBanner: false,
  ));
}
  • 第 2 部分
class _UserRegistrationScreenState extends State<UserRegistrationScreen> {

 DatabaseReference itemRef;
 final firebaseAuth = FirebaseAuth.instance;
 final storage = FirebaseStorage.instance;

 @override
 void initState() {
   super.initState();
   itemRef = FirebaseDatabase.instance.reference().child('Users');

 }
  • 这就是我保存数据的方式
  BuyerModel model = new BuyerModel(
                              fullName,phone,country,state,city,address,
                              email,'online','Buyer', downloadUrl,DateTime.now().microsecondsSinceEpoch.toString(),
                              firebaseAuth.currentUser.uid);
                            itemRef.child(firebaseAuth.currentUser.uid).set(model);
  • 这是买方模型

 class BuyerModel {
  String fullName;
  String phone;
  String country;
  String state;
  String city;
  String address;
  String email;
  String status;
  String accountType;
  String profileImage;
  String timeStamp;
  String userUid;

  BuyerModel(this.fullName,this.phone,this.country,this.state,this.city,
      this.address,this.email,this.status,this.accountType,this.profileImage,
      this.timeStamp,this.userUid);
}

【问题讨论】:

  • 代码乍一看还不错,虽然理想情况下我们应该看到BuyerModel 来确定。或者您可以简化代码。如果itemRef.child(firebaseAuth.currentUser.uid).set(true) 也不起作用,您就知道模型不是问题所在。如果是这种情况,您可能需要检查 logcat 输出中 set() 中的权限错误或附加完成处理程序,如下所示:gist.github.com/puf/4a94a01e3c2510298ee46d0a7f90ab75
  • 您使用flutterkotlin 进行了标记。这是没有意义的,因为您可能只使用其中之一。因此,请更新您的标签,使其仅包含与我们在此处查看的问题相关的内容。
  • 对不起,我把标签'kotlin',因为当我创建一个flutter项目时,它问我除了dart还用什么,我用kotlin,谢谢
  • 这就是问题所在,例如,当我曾经在某处得到空异常时,它会显示,现在它不会在 logcat 中显示任何错误,这就是为什么我一直在尝试..但什么也没有
  • 在 Flutter 中,我认为 Firebase SDK 不知道如何编写像 BuyerModel 这样的自定义类。我通常以fromSnapshottoMap 方法来来回映射。但我希望 SDK 在您传递无法序列化的对象时抛出错误。

标签: firebase flutter dart firebase-realtime-database


【解决方案1】:

在 Flutter 中,我认为 Firebase SDK 不知道如何编写像 BuyerModel 这样的自定义类。我通常以fromSnapshottoMap 方法来来回映射。

此类的一个例子:

class Question {
  HUser owner;
  String title;
  List<String> answers;
  int timeInSeconds;
  String key;

  Question(String this.title, List<String> this.answers, this.timeInSeconds, { this.owner }) {
  }

  Map<String, dynamic> toMap() {
    return <String,dynamic>{
      "owner": owner?.uid,
      "title": title,
      "answers": answers,
      "timeInSeconds": timeInSeconds
    };
  }

  Question.fromSnapshot(DataSnapshot snapshot): assert(snapshot != null),
    title = snapshot.value["title"],
    timeInSeconds = snapshot.value["timeInSeconds"],
    owner = snapshot.value["owner"] != null ? HUser(snapshot.value["owner"]) : null,
    key = snapshot.key;
}

我现在看到我仍然需要从快照中读取answers。这可能解释了我的应用程序中的一个错误。 ?

【讨论】:

  • 非常感谢您的努力和时间,最后一件事,请现在将我的模型传递给 set() 方法,应该是 toMap() ,或者直接像我之前在我的代码?
  • 你应该将toMap()返回值传递给set方法。
  • 我确实这样做了,我复制粘贴了你的模型类,只需要更改为我自己的值,但仍然没有任何效果
  • “没有任何工作”真的很难帮助。我只是以我的课程为例,您肯定需要创建自己的课程来进行类似的转换。当您在执行此操作后单步执行代码时,它是否按照您的预期执行?哪一行是第一行没有?所有变量都有什么值?
  • 我有使用 java 和 kotlin 的 firebase 的经验,它与如何在颤振中做同样的事情有点相似,但我已经添加了所有必要的组件让它工作,因为身份验证和存储工作正常,我有 firebase_database 插件,什么都做了,这让我很困惑,我当时就卡住了
猜你喜欢
  • 2021-05-24
  • 1970-01-01
  • 2021-11-25
  • 2018-06-25
  • 1970-01-01
  • 2021-10-27
  • 2021-05-06
  • 1970-01-01
  • 2021-06-30
相关资源
最近更新 更多