这是我的数据库,对于一个文档,我的检索代码有效,但对于所有其他文档,我收到错误 错误状态:DocumentSnapshotPlatform 中不存在字段。
这是代码。 正在构建的小部件现在仅显示所选文档的文档“温度”字段中的值(使用 docid)。 我不明白它如何适用于一个文档而不适用于其他文档。
widget.which 只是字符串“温度”。我使用它是因为我也在为脉冲做这个,但现在,我只有温度传感器。
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
import 'package:geolocator/geolocator.dart';
import 'package:sms_maintained/contact.dart';
import 'package:sms_maintained/generated/i18n.dart';
import 'package:sms_maintained/globals.dart';
import 'package:sms_maintained/sms.dart';
CollectionReference main = FirebaseFirestore.instance.collection('maindb');
SmsMessage message;
class biom extends StatefulWidget {
biom({this.which, this.image, this.docid});
final String which;
final String docid;
final String image;
@override
_biomState createState() => _biomState();
}
_callNumber(phoneno) async {
print('in calling function');
bool res = await FlutterPhoneDirectCaller.callNumber(phoneno.toString());
}
sendMessage(phoneno, curLat, curLong, flag) async {
print('in sendmessage func');
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
curLat = position.latitude;
curLong = position.longitude;
//flag==0 is temp exceeds
//flag==1 is distance outside
SmsSender sender = SmsSender();
String address = phoneno.toString();
if (flag == 1) {
print('in flag 1');
message = SmsMessage(
address, 'PATIENT HAS WANDERED: LOCATION IS $curLat ,$curLong ');
} else {
print('in flag 0');
message = SmsMessage(address,
'PATIENTS TEMPERATURE IS AT DANGEROUS LEVEL. PROVIDE AMBULANCE TO $curLat ,$curLong');
}
message.onStateChanged.listen((state) {
if (state == SmsMessageState.Sent) {
print("SMS is sent!");
} else if (state == SmsMessageState.Delivered) {
print("SMS is delivered!");
}
});
sender.sendSms(message);
}
distanceCalc(double lat, double long, int radius, phoneno) async {
print('in function with $lat, $long');
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
double curLat = position.latitude;
double curLong = position.longitude;
print(
'current lat is ${position.latitude},current long is ${position.longitude}');
double distanceInMeters =
await Geolocator.distanceBetween(lat, long, curLat, curLong);
print('distance is $distanceInMeters meters');
if (distanceInMeters > radius) {
print('distance exceeds radius');
sendMessage(phoneno, curLat, curLong, 1);
_callNumber(phoneno);
}
}
class _biomState extends State<biom> {
@override
Widget build(BuildContext context) {
print('docid is ${widget.docid}');
return Expanded(
child: Container(
height: 80.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Image.asset('images/${widget.image}.png',
height: 100.0, width: 100.0),
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('maindb')
.doc(widget.docid)
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
}
var userDocument = snapshot.data;
var phoneno = userDocument['phone'];
int radius = userDocument['radius'];
distanceCalc(userDocument['latitude'],
userDocument['longitude'], radius, phoneno);
if (userDocument['temperature'] > 29) {
print('temp is userDocument["${widget.which}"]');
sendMessage(phoneno, 0, 0, 0);
// _callNumber(phoneno);
print(
'calling because ${userDocument["${widget.which}"]} is greater than 31');
}
return CircleWidget(num: userDocument['temperature']);
})
],
),
),
);
}
}
class CircleWidget extends StatelessWidget {
final int num;
CircleWidget({this.num});
@override
Widget build(BuildContext context) {
return Container(
height: 150.0,
width: 200.0,
padding: const EdgeInsets.symmetric(),
decoration: BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.only(
topRight: Radius.circular(40.0),
bottomRight: Radius.circular(40.0),
topLeft: Radius.circular(40.0),
bottomLeft: Radius.circular(40.0))),
child: Center(
child:
Text(num.toString(), style: Theme.of(context).textTheme.headline5),
),
);
}
}
回答************ 用snapshot.data.data()替换snapshot.data
答案 0 :(得分:1)
snapshot.data
应该是 snapshot.data.docs
然后迭代它们并得到
类似let Map<String, dynamic> doc = snapshot.data.docs[i].data().
旁注:更改您的命名约定。大写的类名,变量的驼峰式大小写等
编辑:尝试使用 snapshot.data.data()