Home > Net >  how to pass argument to a final variable in flutter
how to pass argument to a final variable in flutter

Time:10-10

I have the following code and could not figure out what is the problem. I want to pass the Bluetooth device to the container in the class DeviceData and this class returns the container. I am beginner in Flutter and when I read the documentation could not understand what they are talking about.

But the first problem is

Can't define the 'const' constructor because the field 'uppersection' is initialized with a non-constant value. Try initializing the field to a constant value, or removing the keyword 'const' from the

and then device cannot be accessed:

The instance member 'device' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression

class DeviceData extends StatelessWidget {
  const DeviceData({Key key, this.device}) : super(key: key);
  final BluetoothDevice device;

  final uppersection = new Container(
    child: Row(
      children: <Widget>[
        StreamBuilder<BluetoothDeviceState>(
          stream: device.state,
          initialData: BluetoothDeviceState.connecting,
          builder: (c, snapshot) => ListTile(
            leading: (snapshot.data == BluetoothDeviceState.connected)
                ? Icon(Icons.bluetooth_connected)
                : Icon(Icons.bluetooth_disabled),
            title: Text('Device is ${snapshot.data.toString().split('.')[0]}.'),
            subtitle: Text('${device.id}'),
            trailing: StreamBuilder<bool>(
              //The below stream is to show either a refresh button or
              //CircularProgress based on the service discovering status,
              //and a IndexedStack widget is used to present the needed widget
              stream: device.isDiscoveringServices,
              initialData: false,
              builder: (c, snapshot) => IndexedStack(
                index: snapshot.data ? 0 : 0,
                children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.refresh),
                    onPressed: () => device.discoverServices(),
                  ),
                  IconButton(
                    icon: SizedBox(
                      child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation(Colors.grey),
                      ),
                      width: 17.0,
                      height: 17.0,
                    ),
                    onPressed: null,
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    ),
  );

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: Column(
        children: <Widget>[
          uppersection,
        ],
      ),
    );
  }
}

CodePudding user response:

You should rather make upperSection a getter.

Like so:

Container get upperSection{
  return Container(
    child: Row(
      children: <Widget>[
        StreamBuilder<BluetoothDeviceState>(
          stream: device.state,
          initialData: BluetoothDeviceState.connecting,
          builder: (c, snapshot) => ListTile(
            leading: (snapshot.data == BluetoothDeviceState.connected)
                ? Icon(Icons.bluetooth_connected)
                : Icon(Icons.bluetooth_disabled),
            title: Text('Device is ${snapshot.data.toString().split('.')[0]}.'),
            subtitle: Text('${device.id}'),
            trailing: StreamBuilder<bool>(
              //The below stream is to show either a refresh button or
              //CircularProgress based on the service discovering status,
              //and a IndexedStack widget is used to present the needed widget
              stream: device.isDiscoveringServices,
              initialData: false,
              builder: (c, snapshot) => IndexedStack(
                index: snapshot.data ? 0 : 0,
                children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.refresh),
                    onPressed: () => device.discoverServices(),
                  ),
                  IconButton(
                    icon: SizedBox(
                      child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation(Colors.grey),
                      ),
                      width: 17.0,
                      height: 17.0,
                    ),
                    onPressed: null,
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    ),
  );
}
  • Related