当前位置: 首页 > 知识库问答 >
问题:

我如何连接我的ESP8266使用arduino到我的Flutter应用程序。在这里完成初学者

贲凌
2023-03-14

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

共有1个答案

都阳
2023-03-14

检查错误时,它是由字符串变量上指定的预期未来类型引起的。声明的其他变量也会出现类似的错误。您可以将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');

现在要调用异步函数,您可以在上等待它们的结果,然后(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'));

编辑:要添加,您可能还需要考虑使用<代码> WiFixIfFoFiffs插件,它比 WiFi插件更为新近。

 类似资料:
  • 我试图将我的Flitter应用程序与firebase连接,但我发现了这个错误。提前感谢:) 失败:生成失败,出现异常。 错误:配置根项目“android”时出现问题 无法解析配置: classpath的所有工件。无法解决com.google.gms: google-service: 4.3.8.所需:项目: > 尝试:使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--deb

  • 我正在尝试将本地Spring Boot应用程序连接到我的Heroku Postgres数据库。当我使用Heroku上可用的creds尝试此操作时,我得到以下错误 错误:SEVERITY_LOCALIZED=FATAL,SEVERITY_NON_LOCALIZED=FATAL, CODE=28000, MESSAGE=没有主机"myhost",用户"myuser",数据库"mydb"的pg_hba.

  • 我想使用package.json中的代理将我的后端连接到react应用程序,但我在react应用程序的首页显示了一个错误“无效的主机头” 我尝试了这些“代理”:“http://localhost:5000”,然后我尝试了这些“代理”:“https://5000-kcpele-shop clone 1-7 vpnj 3r 84 GC . ws-eu38 . git pod . io/”我希望它能够连

  • 我正在制作一个用户登录应用程序,当执行模拟器时,我可以注册用户,验证用户在我的数据库中的股票,但是当我在手机上执行应用程序时,它不能连接到我的数据库。 这是我与数据库的连接代码:

  • 用户: 而此错误显示为:

  • 当我按住一个按钮时,我很难让我的程序不断发送字符串。我将按钮设置为自动重复,但它仅在按钮释放时发送,而不是在按住时发送。然而,当按住时,计数器会添加应该发送消息的次数。我迷路了。 主窗口。cpp公司 主窗口。h类 根据@dtech建议添加代码 释放按钮后,Arduino中的串行监视器显示发送的所有内容,机器人移动