Firebase Query 不适用于 Flutter,如何解决?
Posted
技术标签:
【中文标题】Firebase Query 不适用于 Flutter,如何解决?【英文标题】:Firebase Query not working with Flutter, How to solve this? 【发布时间】:2020-06-26 20:10:37 【问题描述】:我正在尝试向我的 Flutter 应用程序添加搜索功能,代码中一切正常,没有显示错误,但是当我在应用程序屏幕上搜索用户名时没有显示结果,但有时它会引发错误,提示“失败断言:第 269 行 pos 10: 'data != null' 相关的导致错误的小部件是 FutureBuilder" 尤其是当我使用大写字母并且文本中有空格时。
我附上了 search.dart 和 user.dart 文件的代码。请帮帮我,我被困在这里了。
search.dart 文件
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:social_share/models/user.dart';
import 'package:social_share/pages/home.dart';
import 'package:social_share/widgets/progress.dart';
class Search extends StatefulWidget
@override
_SearchState createState() => _SearchState();
class _SearchState extends State<Search>
TextEditingController searchController = TextEditingController();
Future<QuerySnapshot> searchResultsFuture;
handleSearch(String query)
Future<QuerySnapshot> users = usersRef
.where("displayName", isGreaterThanOrEqualTo: query)
.getDocuments();
setState(()
searchResultsFuture = users;
);
clearSearch()
searchController.clear();
AppBar buildSearchField()
return AppBar(
backgroundColor: Colors.white,
title: TextFormField(
controller: searchController,
decoration: InputDecoration(
hintText: "Search for a user...",
filled: true,
prefixIcon: Icon(
Icons.account_box,
size: 28.0,
),
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: clearSearch,
),
),
onFieldSubmitted: handleSearch,
),
);
Container buildNoContent()
final Orientation orientation = MediaQuery.of(context).orientation;
return Container(
child: Center(
child: ListView(
shrinkWrap: true,
children: <Widget>[
SvgPicture.asset(
'assets/images/search.svg',
height: orientation == Orientation.portrait ? 300.0 : 200.0,
),
Text(
"Find Users",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.w600,
fontSize: 60.0,
),
),
],
),
),
);
buildSearchResults()
return FutureBuilder(
future: searchResultsFuture,
builder: (context, snapshot)
if (!snapshot.hasData)
return circularProgress();
List<Text> searchResults = [];
snapshot.data.documents.forEach((doc)
User user = User.fromDocument(doc);
searchResults.add(Text(user.username , style: TextStyle(color: Colors.black, fontSize: 20.0),));
);
return ListView(
children: searchResults,
);
,
);
@override
Widget build(BuildContext context)
return Scaffold(
backgroundColor: Theme.of(context).primaryColor.withOpacity(0.8),
appBar: buildSearchField(),
body:
searchResultsFuture == null ? buildNoContent() : buildSearchResults(),
);
class UserResult extends StatelessWidget
@override
Widget build(BuildContext context)
return Text("User Result");
user.dart 文件
import 'package:cloud_firestore/cloud_firestore.dart';
class User
final String id;
final String username;
final String email;
final String photoUrl;
final String displayName;
final String bio;
//Constructor
User(
this.id,
this.username,
this.email,
this.photoUrl,
this.displayName,
this.bio,
);
factory User.fromDocument(DocumentSnapshot doc)
return User(
id: doc['id'],
email: doc['email'],
username: doc['username'],
photoUrl: doc['photoUrl'],
displayName: doc['displayName'],
bio: doc['bio'],
);
Firebase 数据库数据截图
Firebase Users Data Screenshot
【问题讨论】:
【参考方案1】:如果看起来如果实际数据为空,你会得到一个错误。
尝试:
if(!snapshot.hasData || snapshot.data?.documents == null)
代替:
if (!snapshot.hasData)
参考 - https://github.com/flutter/flutter/issues/22199
【讨论】:
您好,感谢您的回复。我已尝试以下说明,但应用程序屏幕上未显示搜索结果。仅在应用启动时在调试控制台上打印两行I/flutter (29393): Instance of 'User' I/flutter (29393): null
【参考方案2】:
已解决
在我将 if 语句更改为以下行之后,它正在工作。
if(!snapshot.hasData || snapshot.data.documents == null)
【讨论】:
以上是关于Firebase Query 不适用于 Flutter,如何解决?的主要内容,如果未能解决你的问题,请参考以下文章
ios UIApplicationOpenURLOptionsKey 不适用于 Firebase