我尝试了本教程 https://www.youtube.com/watch?v=ZiagJJTqnZQ
但是我的数据没有显示,长度仍然显示0
这是我的火力发源地https://i.imgur.com/D6kBpp8.png
代码
questions.dart
class Questions {
String question, questioner, status;
Questions(this.question, this.questioner, this.status);
}
timeline.dart
import 'package:flutter/material.dart';
import 'questions.dart';
import 'package:firebase_database/firebase_database.dart';
class TimeLine extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _TimeLineState();
}
}
class _TimeLineState extends State<TimeLine> {
final primaryColor = const Color(0xFF006FB9);
final bgColor = const Color(0xFFFEFEFE);
List<Questions> questionsList = [];
@override
void initState() {
super.initState();
DatabaseReference questionsRef = FirebaseDatabase.instance.reference().child("Questions");
questionsRef.once().then((DataSnapshot snap)
{
var KEYS = snap.value.keys;
var DATA = snap.value;
questionsList.clear();
for(var individualKey in KEYS) {
Questions questi = new Questions(
DATA[individualKey]['question'],
DATA[individualKey]['questioner'],
DATA[individualKey]['status'],
);
questionsList.add(questi);
}
setState(() {
print('Length : $questionsList.length');
});
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 1,
title: Text('Discover'),
backgroundColor: primaryColor,
),
backgroundColor: bgColor,
body: Container(
child: Text(
questionsList.length.toString()
)
/*questionsList.length == 0 ? new Text("No Blog Post Available") : new ListView.builder(
itemCount: questionsList.length,
itemBuilder: (_, index) {
return QuestionsGrid(questionsList[index].question, questionsList[index].questioner, questionsList[index].status);
}
),*/
),
);
}
Widget QuestionsGrid(String question, String questioner, String status) {
return new Container(
height: 1000,
width: 1000,
child: Text(
question
),
);
}
}
答案 0 :(得分:1)
您的代码很完美,但是您必须像这样更改Firebase代码行
DatabaseReference questionsRef = FirebaseDatabase.instance.reference().child("questions");
了解
DatabaseReference questionsRef = FirebaseDatabase.instance.reference().child("Questions");
谢谢