0

I'm working on a flutter project that receives data from a ble device, when I created the project It worked but now I copied the program to another project and there's an error that I don't know how to solve. the error is in this line BluetoothDevice _connectedDevice;

where the error is Non-nullable instance field '_connectedDevice' must be initialized.

1 Answers1

0

I am working on a similar project. The problem I faced was the the Field device@.... was not initiated , I used "late" for variable initialization and it does not get initiated . there are 2 solutions

  1. use ? instead of late but chances are your variable will show null value

2 initialize the variable by assigning it some value of same type like this

late String name;
@override
Widget build(BuildContext context) {
  return Scaffold(
        body: Text(name)
        //runtime error: 
        //LateInitializationError: Field 'name' has not been initialized.
  );
} 

becomes:

late String name;

@override
void initState() {
  name = "Flutter Campus";// the type of value you assign should be same like in this case string
  super.initState();
}
  
@override
Widget build(BuildContext context) {
  return Scaffold(
        body: Text(name)
  );
} 
  • If I am incorrect please correct me . I haven't solved my own problem because I don't know what value to assign to BluetoothDevice – glock-a-mole Jun 08 '22 at 06:08