IoT Project ( Flutter + Python + ZigBee ) 冒险闯关记 -- Part 3

In time, you will call me master -- Star Wars

刚入门flutter,大概有1个月的经验,做过一些小程式练习,已经可以写得动project了,而且我满喜欢flutter UI的.http://img2.58codes.com/2024/emoticon12.gif

开始吧,上次讲到资料成功上传到firestore,但其实一开始我是把资料放在realtime firebase的,也只花一天就做出拿资料显示图表分析,但在关掉app重开时,就有error显示

Unhandled Exception: MissingPluginException(No implementation found for method Query#observe on channel plugins.flutter.io/firebase_database)

花了2天才解决https://i.stack.imgur.com/i49F4s.png

后来从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 设定的目标,有空会继续往下做
希望之后写出来的东西,也能解释的很好.http://img2.58codes.com/2024/emoticon41.gif


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章