I want to develop an app for requesting some datas from a device with modbus TCP /IP. I used flutter in order to create it but I have an error:
E/flutter ( 7330): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'add' was called on null
In attachment there are the flie...would you like to help me,please? Any help will be very useful....
Thank you!!!!
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();
}