Flutter Modbus TCPIP颤振/省道异常错误
我想开发一个应用程序,用于从具有modbus TCP/IP的设备请求一些数据。我使用颤振来创建它,但我有一个错误: E/flatter(7330):[ERROR:flatter/lib/ui/ui\u dart\u state.cc(166)]未处理的异常:NoSuchMethodError:对null调用了方法“add” 附件里有这张单子……你能帮我吗? 任何帮助都是非常有用的 谢谢Flutter Modbus TCPIP颤振/省道异常错误,flutter,dart,modbus-tcp,Flutter,Dart,Modbus Tcp,我想开发一个应用程序,用于从具有modbus TCP/IP的设备请求一些数据。我使用颤振来创建它,但我有一个错误: E/flatter(7330):[ERROR:flatter/lib/ui/ui\u dart\u state.cc(166)]未处理的异常:NoSuchMethodError:对null调用了方法“add” 附件里有这张单子……你能帮我吗? 任何帮助都是非常有用的 谢谢 main.dart import 'package:primo_schermo/Schermi.dart';
main.dart
import 'package:primo_schermo/Schermi.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData.dark(),
home: MyHomePage(title: 'Flutter Demo Home Page'),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
initScreen();
}
@override
void dispose() {
super.dispose();
releaseResource();
}
build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: new Text(' XT MONITOR'),
),
body: OrientationBuilder(builder: (context, orientation) {
if (orientation == Orientation.portrait) {
return potraitFirstScreen(context);
} else {
return landscapeFirstScreen(context);
}
}),
);
}
}
Schermi.dart
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:modbus/modbus.dart' as modbus;
String address = '127.0.1.1';
int port = 400;
bool verifylenghtport = false;
bool verifylenghtaddr = false;
//UserModbus userModbus;
TextEditingController usraddress = TextEditingController();
TextEditingController usrport = TextEditingController();
Widget landscapeFirstScreen(BuildContext context) {
return Container(
child: ListView(
children: <Widget>[
// Logo DKC
Container(
height: MediaQuery.of(context).size.height * 0.25,
width: MediaQuery.of(context).size.width * 0.2,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/Dkc.png'),
fit: BoxFit.cover,
),
),
),
// Textfield insert ip
Container(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.redAccent,
),
borderRadius: BorderRadius.circular(8.0),
),
hintText: "Insert IP UPS Address...",
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
],
controller: usraddress,
onChanged: (text) {
if (usraddress.text.length > 4) {
verifylenghtaddr = true;
} else {
verifylenghtaddr = false;
}
},
),
),
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.02,
),
// Textfield insert port
Container(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.redAccent,
),
borderRadius: BorderRadius.circular(8.0),
),
hintText: "Insert Port...",
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
],
controller: usrport,
onChanged: (text) {
if (usrport.text.length == 3) {
verifylenghtport = true;
} else {
verifylenghtport = false;
}
},
),
),
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.02,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
ButtonTheme(
minWidth: MediaQuery.of(context).size.width * 0.98,
height: MediaQuery.of(context).size.height * 0.13,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.black,
textColor: Colors.red,
padding: EdgeInsets.all(10.0),
onPressed: enableconnectionbutton() ? connectUPS : null,
child: Text(
"Connect".toUpperCase(),
style: TextStyle(
fontSize: 14.0,
),
),
),
),
]),
],
),
);
}
/*
* Widget portratit
*
*/
Widget potraitFirstScreen(BuildContext context) {
return Container(
child: ListView(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.45,
width: MediaQuery.of(context).size.width * 0.35,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/Dkc.png'),
fit: BoxFit.cover,
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.redAccent,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: "Insert IP UPS Address...",
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
],
controller: usraddress,
onChanged: (text) {
if (usraddress.text.length > 4) {
verifylenghtaddr = true;
} else {
verifylenghtaddr = false;
}
},
),
),
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.1,
),
Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.redAccent,
),
borderRadius: BorderRadius.circular(8.0),
),
hintText: "Insert Port...",
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
],
controller: usrport,
onChanged: (text) {
if (usrport.text.length == 3) {
verifylenghtport = true;
} else {
verifylenghtport = false;
}
},
),
),
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.04,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
ButtonTheme(
minWidth: MediaQuery.of(context).size.width * 0.89,
height: 20,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.black,
textColor: Colors.red,
padding: EdgeInsets.all(10.0),
onPressed: enableconnectionbutton() ? connectUPS : null,
child: Text(
"Connect".toUpperCase(),
style: TextStyle(
fontSize: 14.0,
),
),
),
),
]),
],
),
);
}
void initScreen() {
usraddress.addListener(remCharAddr);
usrport.addListener(remCharPort);
}
void remCharPort() {
String cr = "\r";
final text =
usrport.text.replaceAll("-", "").replaceAll(",", "").replaceAll(cr, "");
usrport.value = usrport.value.copyWith(
text: text,
selection:
TextSelection(baseOffset: text.length, extentOffset: text.length),
composing: TextRange.empty);
}
void remCharAddr() {
String cr = "\r";
final text = usraddress.text
.replaceAll("-", "")
.replaceAll(",", "")
.replaceAll(cr, "");
usraddress.value = usraddress.value.copyWith(
text: text,
selection:
TextSelection(baseOffset: text.length, extentOffset: text.length),
composing: TextRange.empty);
}
void releaseResource() {
usraddress.dispose();
usrport.dispose();
}
void connectUPS() {
//userModbus.address = usraddress.text;
address = usraddress.text;
port = int.parse(usrport.text);
//print("Address:" + userModbus.address);
//print("Port:" + userModbus.port.toString());
//connect(userModbus.address, userModbus.port);
connecttoUPS(address, port);
}
bool enableconnectionbutton() {
if (verifylenghtaddr && verifylenghtport) {
return true;
} else {
return false;
}
}
void connecttoUPS(String address, int porta) async {
//int error = 0;
//String ip = '127.0.0.1';
//int p = 502;
var client =
modbus.createTcpClient(address, port: porta, mode: modbus.ModbusMode.rtu);
try {
await client.connect();
} finally {
var registers = await client.readInputRegisters(0x0006, 4);
for (int i = 0; i < registers.length; i++) {
print("REG_I:" + registers.elementAt(i).toString());
}
}
client.close();
}
main.dart
导入“包:primo_schermo/Schermi.dart”;
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:ThemeData.dark(),
主页:MyHomePage(标题:“颤振演示主页”),
debugShowCheckedModeBanner:false,
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
void initState(){
super.initState();
initScreen();
}
@凌驾
无效处置(){
super.dispose();
释放资源();
}
构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.red,
标题:新文本(“XT监视器”),
),
正文:方向生成器(生成器:(上下文,方向){
如果(方向==方向.纵向){
返回potraitFirstScreen(上下文);
}否则{
返回landscapeFirstScreen(上下文);
}
}),
);
}
}
雪米镖
进口“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”;
导入“包:flifter/services.dart”;
将“包:modbus/modbus.dart”作为modbus导入;
字符串地址='127.0.1.1';
int端口=400;
bool verifylenghtport=false;
bool verifylenghtaddr=false;
//用户Modbus用户Modbus;
TextEditingController usradress=TextEditingController();
TextEditingController usrport=TextEditingController();
小部件landscapeFirstScreen(构建上下文){
返回容器(
子:ListView(
儿童:[
//标志DKC
容器(
高度:MediaQuery.of(上下文).size.height*0.25,
宽度:MediaQuery.of(context).size.width*0.2,
装饰:盒子装饰(
图像:装饰图像(
image:AssetImage('images/Dkc.png'),
适合:BoxFit.cover,
),
),
),
//文本字段插入ip
容器(
孩子:填充(
填充:常数边集全部(2.0),
孩子:TextField(
textAlign:textAlign.center,
装饰:输入装饰(
enabledBorder:OutlineInputBorder(
边界边(
颜色:Colors.redAccent,
),
边界半径:边界半径。圆形(8.0),
),
hintText:“插入IP UPS地址…”,
),
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringInputFormatter.allow(RegExp(r'[0-9.]'),
],
控制员:美国海军陆战队,
一旦更改:(文本){
如果(usradress.text.length>4){
verifylenghtaddr=true;
}否则{
verifylenghtaddr=false;
}
},
),
),
),
大小盒子(
高度:MediaQuery.of(上下文).size.width*0.02,
),
//Textfield插入端口
容器(
孩子:填充(
填充:常数边集全部(2.0),
孩子:TextField(
textAlign:textAlign.center,
装饰:输入装饰(
enabledBorder:OutlineInputBorder(
边界边(
颜色:Colors.redAccent,
),
边界半径:边界半径。圆形(8.0),
),
hintText:“插入端口…”,
),
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringInputFormatter.allow(RegExp(r'[0-9.]'),
],
控制器:usrport,
一旦更改:(文本){
if(usrport.text.length==3){
verifylenghtport=true;
}否则{
verifylenghtport=false;
}
},
),
),
),
大小盒子(
高度:MediaQuery.of(上下文).size.width*0.02,
),
行(mainAxisAlignment:mainAxisAlignment.center,子项:[
钮扣(
minWidth:MediaQuery.of(context).size.width*0.98,
高度:MediaQuery.of(上下文).size.height*0.13,
孩子:扁平按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(18.0),
边:边框边(颜色:Colors.red)),
颜色:颜色,黑色,
textColor:Colors.red,
填充:所有边缘设置(10.0),
onPressed:enableconnectionbutton()?connectUPS:null,
子:文本(
“Connect”.toUpperCase(),
样式:TextStyle(
字体大小:14.0,
),
),
),
),
]),
],
),
);
}
/*
*小工具portratit
*
*/
小部件potraitFirstScreen(构建上下文){
返回容器(
子:ListView(
儿童:[
容器(
高度:MediaQuery.of(上下文).size.height*0.45,
宽度:MediaQuery.of(context).size.width*0.35,
装饰:盒子装饰(
图片:DecorationImag