【问题标题】:How do I retrive firebase data in flutter如何在颤振中检索 Firebase 数据
【发布时间】:2019-06-11 04:11:50
【问题描述】:

我正在尝试从 firebase 数据库中获取数据列表并将其显示为我的颤振应用程序中的列表,目前我无法在 UI 上显示数据。当我尝试打印列表时当它应该显示我在数据库中输入的名称时不包含任何值。

这是我要检索的数据库:

这是我为我的 UI 实现的代码,关于这里的问题有什么想法吗?

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'SignInSuccessPage.dart';

class SignInPage extends StatefulWidget {
  @override
  SignInPageState createState() => SignInPageState();
}

class SignInPageState extends State<SignInPage> {
  List<Volunteer> volunteers;
  Volunteer volunteer;
  DatabaseReference volunteerRef;


  @override
  void initState() {
    super.initState();
    volunteers = new List();
    volunteer = Volunteer("","", "");
    final FirebaseDatabase database = FirebaseDatabase.instance; /
    volunteerRef = database.reference().child('volunteerapp-cec4f');
    volunteerRef.onChildAdded.listen(_onEntryAdded);
    volunteerRef.onChildChanged.listen(_onEntryChanged);

  }

  _onEntryAdded(Event event) {
    setState(() {
      volunteers.add(Volunteer.fromSnapshot(event.snapshot));
    });
  }

  _onEntryChanged(Event event) {
    var old = volunteers.singleWhere((entry) {
      return entry.key == event.snapshot.key;
    });
    setState(() {
      volunteers[volunteers.indexOf(old)] = Volunteer.fromSnapshot(event.snapshot);
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('List of Names'),
      ),
      resizeToAvoidBottomPadding: false,
      body: Column(
        children: <Widget>[
          Flexible(
            child: FirebaseAnimatedList(
              query: volunteerRef,
              itemBuilder: (BuildContext context, DataSnapshot snapshot,
                  Animation<double> animation, int index) {
                return new ListTile(
                  title: Text(volunteers[index].firstName),
                  subtitle: Text(volunteers[index].lastName),

                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

//the volunteer class which contains all the info about each volunteer object and links to firebase
class Volunteer {
  String key;
  String firstName;
  String lastName;

  Volunteer(this.key, this.firstName, this.lastName);

  Volunteer.fromSnapshot(DataSnapshot snapshot)
      : key = snapshot.key,
        firstName = snapshot.value["firstName"],
        lastName = snapshot.value["lastName"];

  toJson() {
    return {
      "key": key,
      "firstName": firstName,
      "lastName": lastName,
    };
  }
}

【问题讨论】:

    标签: firebase firebase-realtime-database dart flutter


    【解决方案1】:

    您无需将项目名称作为child 调用包含在内,它已经在基本引用中。

    所以:

    volunteerRef = database.reference();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-03
      • 2020-04-15
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2021-12-09
      • 2020-08-20
      • 2020-08-13
      相关资源
      最近更新 更多