在这里,我正在尝试从Firestore获取地点名称。我做了一个getplaces函数,该函数从firestone导入文件并将它们放在列表中。为了在标签栏上显示位置。
问题是:即使Iam在您的init状态下调用该函数,它也会返回“带有列表的温度” 的错误超出了范围0的范围,即使当我打印温度列表时它也会正确打印位置。
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';
class HomePage extends StatefulWidget {
// const HomePage({Key key}) : super(key: key);
@override
HomePageState createState() {
return new HomePageState();
}
}
class HomePageState extends State<HomePage> {
List<String> temp=new List();
List<String> temp1=['Nile University', 'Smart Village', 'Zewail'];
@override
void initState() {
super.initState();
getplaces(temp);
}
Future<List> getplaces(temp) async{
QuerySnapshot place= await Firestore.instance.collection('places').getDocuments();
place.documents.forEach((DocumentSnapshot doc){
temp.add(
doc.data['name']
);
print(doc.data['name']);
});
print(temp);
return temp;
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 20,
child: Scaffold(
appBar: AppBar(
title: Text("Home Page"),
bottom: TabBar( isScrollable: true,
tabs: new List.generate(temp.length, (index) {
return new Tab(child: Text(temp[index].toString().toUpperCase()));
}),),),
body: !temp.isNotEmpty?
Center(child: CircularProgressIndicator()):
TabBarView(
children: <Widget>[
Text(temp[0].toString()),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
)));
}}
//String f=snapshot.data.documents[index]["Place"].toString();
// print( Firestore.instance.collection("Places").document(f).collection("order 1").document('order 1').snapshots().toString());
// return TabBar(
// isScrollable:true,
// tabs: ListView.builder(
// itemCount: snapshot.data.documents.length,
// itemBuilder: (context,int index)=>
// Tab(text: snapshot.data.documents[index]["Place"].toString().toUpperCase())
// )) ;
// return ListView.builder(
// itemCount: snapshot.data.documents.length,
// itemBuilder: (context,int index)=>
// FlatButton(
// child: Row(
// children: <Widget>[
// Flexible(child: Text(snapshot.data.collections[index].toString())),
// ],
// ),
// onPressed:null,
// padding: EdgeInsets.fromLTRB(25.0, 10.0, 25.0, 10.0),
// shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
// ),
// );
// String s=snapshot.data.documents[0]["Place"].toString();
// print("place is $s"