In time, you will call me master -- Star Wars
刚入门flutter,大概有1个月的经验,做过一些小程式练习,已经可以写得动project了,而且我满喜欢flutter UI的.
开始吧,上次讲到资料成功上传到firestore
,但其实一开始我是把资料放在realtime firebase
的,也只花一天就做出拿资料显示图表分析,但在关掉app重开时,就有error显示
Unhandled Exception: MissingPluginException(No implementation found for method Query#observe on channel plugins.flutter.io/firebase_database)
花了2天才解决
后来从realtime firebase 换到 firestore , 并且不要按照firebase指示,跳过步奏4,5,不需要在AppDelegate.swift
加上任何程式码.
Flutter 程式部分
从firestore拿取资料拿下来data 存到Map
的 data structure 里, key:是第几笔资料 value:是温度跟亮度用 fl_chart
显示资料图表特别的只有一点 fl_chart
用的是 FlSpot
来存放显示图表的x,y轴但package範例里 fl_chart
是直接放data写死的所以要这样 List<FlSpot>
之后再把Map
的温度,亮度放进去The snapshots() method returns a Stream, therefore you can call the method listen() that will subscribe to the stream and keep listening for any changes in Cloud Firestore.
import 'package:cloud_firestore/cloud_firestore.dart';final databaseReference = Firestore.instance;// create a Map to store key time seriesvar time_temp_dict = new Map();var time_light_dict = new Map();databaseReference .collection("iot-zigbee") .snapshots() // listen for realtime updates .listen((result) { result.documentChanges.forEach((res) { if (res.type == DocumentChangeType.added) { // limit data display in chart to only 10 if (cnt>10){ temp.clear(); light.clear(); time_temp_dict.clear(); time_light_dict.clear(); cnt = 0; } print("added"); // get temperature data print(res.document.data["temperature"]); // get illuminance data print(res.document.data["illuminance"]); temp.add(res.document.data["temperature"].toString()); // Map data type key : count number value : temperature time_temp_dict[cnt] = res.document.data["temperature"].toString(); light.add(res.document.data["illuminance"].toString()); // Map data type key : count number value : illuminance time_light_dict[cnt] = res.document.data["illuminance"].toString(); cnt+=1; }
以上程式码,都不是完整的,只是大概的样子,之后有放到GitHub再附上连结
reference : https://medium.com/firebase-tips-tricks/how-to-use-cloud-firestore-in-flutter-9ea80593ca40
心得 : 可以做出来,但自己不太会解释自己怎么做的,当下写程式很清楚,过几天就比较模糊了,目前完成 phase 1 设定的目标,有空会继续往下做
希望之后写出来的东西,也能解释的很好.