如何使用arduino将我的ESP8266连接到Flutter App。在此处完成初学者

时间:2019-01-17 07:34:56

标签: arduino flutter esp8266

我想通过wifi将一些数据从Arduino-uno发送到我的flutter应用程序。我正在使用ESP8266 wifi模块。我试图找到一些在线图书馆,我发现了wifi|Flutterconnectivity|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,然后将其发送到flutter应用程序。 我和我的团队都是初学者,所以请原谅任何愚蠢的错误,

1 个答案:

答案 0 :(得分:0)

检查错误,这是由于在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插件要新。