我正在尝试制作流畅的Web应用程序。但是问题出在哪里,无论何时我刷新页面或第一次出现错误时出现在屏幕上,然后过一会儿检索值就出现在屏幕上并且错误消失了。
以下:
在构建StreamBuilder时引发NoSuchMethodError(脏,状态: _StreamBuilderBaseState>#6203e):
'文档'
找不到方法
接收者:null
参数:[]
我想知道为什么这种行为在页面上发生?问题出在哪里?
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fulltext_search/study.dart';
import 'package:fulltext_search/study_search.dart';
import 'package:fulltext_search/visitor.dart';
void main() {
runApp(StudentSearchData());
}
class StudentSearchData extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var queryResultSet = [];
var tempSearchStore = [];
initiateSearch(value) {
if (value.length == 0) {
setState(() {
queryResultSet = [];
tempSearchStore = [];
});
}
var capitalizedValue = value.substring(0, 1).toUpperCase() + value.substring(1);
var lowerValue = value.substring(0, 1).toLowerCase() + value.substring(1);
if (queryResultSet.length == 0 && value.length == 1) {
SearchService().searchByName(value).then((QuerySnapshot docs) {
for (int i = 0; i < docs.documents.length; ++i) {
queryResultSet.add(docs.documents[i].data);
}
});
} else {
tempSearchStore = [];
queryResultSet.forEach((element) {
if (element['fname'].startsWith(capitalizedValue)) {
setState(() {
tempSearchStore.add(element);
});
}
if (element['fname'].startsWith(lowerValue)) {
setState(() {
tempSearchStore.add(element);
});
}
});
}
}
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
/*24 is for notification bar on Android*/
final double itemHeight = (size.height - kToolbarHeight-24)/3;
final double itemWidth = size.width / 2;
return new Scaffold(
appBar: new AppBar(
title: Text('Student data search'),
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => Student()),);
},
child: Text("Student",),
),
FlatButton(
textColor: Colors.white,
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => Visitor()),);
},
child: Text("Visitor",),
),
FlatButton(
color: Colors.grey[400],
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white,width: 1),
borderRadius: BorderRadius.circular(10),
),
textColor: Colors.black87,
onPressed: () {},
child: Text("History",style: TextStyle(fontSize: 15,fontWeight: FontWeight.bold),),
),
FlatButton(
textColor: Colors.white,
onPressed: () {
//Navigator.push(context, MaterialPageRoute(builder: (context) => VisitorHistory()),);
},
child: Text("Logout",),
),
],
),
body: ListView(children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
onChanged: (val) {
initiateSearch(val);
},
decoration: InputDecoration(
prefixIcon: IconButton(
color: Colors.black,
icon: Icon(Icons.arrow_back),
iconSize: 20.0,
onPressed: () {
Navigator.of(context).pop();
},
),
contentPadding: EdgeInsets.only(left: 25.0),
hintText: 'Search by name',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0))),
),
),
SizedBox(height: 10.0),
GridView.count(
childAspectRatio: (itemWidth / itemHeight),
padding: EdgeInsets.only(left: 10.0, right: 10.0),
crossAxisCount: 2,
//scrollDirection: Axis.horizontal,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
primary: false,
shrinkWrap: true,
children: tempSearchStore.map((element) {
return buildResultCard(element);
}).toList())
]));
}
}
Widget buildResultCard(data) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
child: Card(
color: Colors.white,
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.blue,width: 2),
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width:120,
child: Row(children: <Widget>[
Image.network(data['image'], width:120,height: 120,fit: BoxFit.cover,),
Spacer(),
]),
),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Name:- ${data['fname']}\n', style: new TextStyle(fontSize: 12.0,color: Colors.black),),
Text('Year:- ${data['year']}\n', style: new TextStyle(fontSize: 12.0,color: Colors.black),),
Text('Contact no:- ${data['contact']}\n', style: new TextStyle(fontSize: 12.0,color: Colors.black),),
Text('Vehicle no:- ${data['vehicleno']}\n', style: new TextStyle(fontSize: 12.0,color: Colors.black),),
Row(
children: <Widget>[
Expanded(
child: Text('purpose of visiting:- ${data['purpose']}\n', style: new TextStyle(fontSize: 12.0,),),
),
],
),
Text('Entry-time:- ${data['Entry-time']}\n', style: new TextStyle(fontSize: 12.0,),),
Text('Exit-time:- ${data['Exit-time']}\n', style: new TextStyle(fontSize: 12.0,),),
],
),
)
],
),
),
),
),
),
);
}
答案 0 :(得分:0)
如果数据不为null,则添加if else语句以返回小部件,如果为null,则返回加载ui。
答案 1 :(得分:0)
错误似乎来自以下代码段:
Data:
您对 SearchService().searchByName(value).then((QuerySnapshot docs) {
for (int i = 0; i < docs.documents.length; ++i) {
queryResultSet.add(docs.documents[i].data);
}
});
的调用似乎可以通过searchByName(value)
的{{1}}来解决,然后导致docs
抛出错误。
无法说出null
返回docs.documents
的原因而没有看到代码。
答案 2 :(得分:0)
尝试首先使用 (snapshot.hasData)
检查快照数据,如果快照为空则返回 Circular Progress Indicator
。这将解决这个问题。
答案 3 :(得分:0)
发生这种情况是因为最初快照没有任何数据。可以通过一定的条件检查数据是否已经到达快照。
if(snapshot.hasData){
return MainWidget();
}else if(snapshot.hasData){
return Text("Error in loading data”);
}else{
return SizedBox(
height: 30,
width: 30,
child: CircularProgressIndicator(
),
);
}