arduino - 如何使用 arduino 将我的 ESP8266 连接到我的 Flutter 应用程序。在这里完成初学者
问题描述
我想通过 wifi 将一些数据从我的 Arduino-uno 发送到我的颤振应用程序。我正在使用 ESP8266 wifi 模块。我试图找到一些在线图书馆,我找到了wifi|Flutter和connectivity|Flutter。但是我无法让它运行。
我已经安装了这些包,并将它们添加到 pubspec.yaml 文件中。
dependencies:
flutter:
sdk: flutter
wifi: ^0.1.4
connectivity: ^0.3.2
我还尝试了包存储库中的示例代码,
import 'dart:async';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:wifi/wifi.dart';
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget {
String ssid = await Wifi.ssid;
//Signal strength, 1-3,The bigger the number, the stronger the signal
int level = await Wifi.level;
String ip = await Wifi.ip;
var result = await Wifi.connection('ssid', 'password');
List<WifiResult> list = await Wifi.list('key');
@override
Widget build(BuildContext context) {
return Container(
child: Text('$level $list'),
);
}
}
但遇到一些错误,例如(行号在每行的末尾)
error: A value of type 'Future<String>' can't be assigned to a variable of type 'String'. (invalid_assignment at [all_tests_here1] lib\main.dart:7)
error: Unexpected text 'await'. (unexpected_token at [all_tests_here1] lib\main.dart:7)
error: Unexpected text 'await'. (unexpected_token at [all_tests_here1] lib\main.dart:10)
error: A value of type 'Future<int>' can't be assigned to a variable of type 'int'. (invalid_assignment at [all_tests_here1] lib\main.dart:10)
error: A value of type 'Future<String>' can't be assigned to a variable of type 'String'. (invalid_assignment at [all_tests_here1] lib\main.dart:12)
error: Unexpected text 'await'. (unexpected_token at [all_tests_here1] lib\main.dart:12)
error: Unexpected text 'await'. (unexpected_token at [all_tests_here1] lib\main.dart:14)
error: A value of type 'Future<List<WifiResult>>' can't be assigned to a variable of type 'List<WifiResult>'. (invalid_assignment at [all_tests_here1] lib\main.dart:15)
error: Unexpected text 'await'. (unexpected_token at [all_tests_here1] lib\main.dart:15)
我想使用 wifi 将所有传感器数据(2 个传感器)发送到 firebase,然后将其发送到颤振应用程序。我和我的团队完全是初学者,所以请原谅任何愚蠢的错误,
解决方案
检查错误,它是由在 String 变量上分配预期的 Future 类型引起的。声明的其他变量也会引发类似的错误。您可以将 var 设置为dynamic
或设置预期的类型。
Future<String> getWifiSSID() async => await Wifi.ssid;
Future<int> getWifiSignalLevel() async => await Wifi.level;
Future<String> getWifiIp() async => await Wifi.ip;
Future<WifiState> getWifiAccess(String wifiSSID, String wifiPass) async =>
await Wifi.connection(wifiSSID, wifiPass);
Future<List<WifiResult>> getWifiList() async => await Wifi.list('key');
现在要调用异步函数,您可以等待它们的结果,then(value)
并可选择使用catchError(onError)
.
// Connect to wifi
getWifiAccess(SSID, pass)
.then((wifiAccess) => debugPrint('wifiAccess: $wifiAccess'))
.catchError((onError) => debugPrint('wifiAccess error: $onError'));
// fetch network SSID of current wifi connection
getWifiSSID()
.then((wifiSSID) => debugPrint('wifiSSID: $wifiSSID'))
.catchError((onError) => debugPrint('wifiSSID error: $onError'));
// fetch network signal level of current wifi connection
getWifiSignalLevel()
.then((wifiSignalLevel) => debugPrint('wifiSignalLevel: $wifiSignalLevel'))
.catchError((onError) => debugPrint('wifiSignalLevel error: $onError'));
// fetch network IP address
getWifiIp()
.then((wifiIpAddress) => debugPrint('wifiIpAddress: $wifiIpAddress'))
.catchError((onError) => debugPrint('wifiIpAddress error: $onError'));
// fetch list of access points (only works on Android)
getWifiList().then((wifiList) {
wifiList.forEach((wifi) {
debugPrint('wifiList: ${wifi.ssid} signal: ${wifi.level}');
});
}).catchError((onError) => debugPrint('wifiList error: $onError'));
编辑:要添加,您可能还需要考虑使用与插件wifi_info_flutter
相比更新的wifi
插件。
推荐阅读
- android - Launcher Activity not calling when app came from background to foreground in android
- javascript - Declaring text to a variable shows Text string must be rendered within a
component - c++ - 是否有公式可以找到浮点数中指数或有效位数的位数?
- flutter - 创建单选按钮开/关
- php - Laravel 和 ChartJS
- python - 如何在列表的子列表中查找项目的索引(python)
- oracle - Oracle 到 Hadoop 的迁移
- javascript - 如何让 localStorage 数据持久化
- java - Java:如何在 main 函数中使用 Enum 类型的变量?
- php - 如何为不同国家设置 HTML 语言属性?