Home > Blockchain >  what does it do and error at setstate in flutter
what does it do and error at setstate in flutter

Time:08-25

The error in setState method, I don't get the point the error.

  1. Could you explain that and what does it mean and do? >>> setState((){_ _availableBiometric = availableBiometric });

  2. Is it right way like this? >>> setState(() { _canCheckBiometric = _canCheckBiometric; });

In the below, full code. Thanks.

void main() => runApp(MaterialApp(
        debugShowCheckedModeBanner: false,
          home: FingerprintApp(),
      ));

 class FingerprintApp extends StatefulWidget {
     const FingerprintApp({Key? key}) : super(key: key);

 @override
   State<FingerprintApp> createState() => _FingerprintAppState();
   }


    class _FingerprintAppState extends State<FingerprintApp> {
     LocalAuthentication auth = LocalAuthentication();
     late bool _canCheckBiometric;
    late List<BiometricType> _availableBiometrics; 
   String autherized = "Not autherized"; 
   Future<void> _checkBiometric() async{
     bool canCheckBiometric;
    try{
      canCheckBiometric = await auth.canCheckBiometrics;
     } on PlatformException catch(e) {
       print(e);
    }
     if(!mounted) return;
    setState(() {
        _canCheckBiometric = _canCheckBiometric;
      });
        }


   void _getAvailableBiometrics() async{

       List<BiometricType> availableBiometric;

       try{

         availableBiometric = await auth.getAvailableBiometrics();
      } on PlatformException catch(e) {
         print(e);
        }

       if(!mounted) return;
     setState(() {
       _availableBiometric = availableBiometric.  //Here, It has error. 
      });
     }

CodePudding user response:

sorry about my english. above you defined the list as _availableBiometrics. but you used below as _availableBiometric. I think you forgot the 's'

CodePudding user response:

You should move the setState between the try..catch. It's going to be like the following:

class FingerprintApp extends StatefulWidget {
  const FingerprintApp({Key? key}) : super(key: key);

  @override
  State<FingerprintApp> createState() => _FingerprintAppState();
}

class _FingerprintAppState extends State<FingerprintApp> {
  LocalAuthentication auth = LocalAuthentication();
  late bool _canCheckBiometric;
  late List<BiometricType> _availableBiometrics;
  String autherized = "Not autherized";
  Future<void> _checkBiometric() async {
    bool canCheckBiometric;
    try {
      canCheckBiometric = await auth.canCheckBiometrics;
      if (!mounted) return;
      setState(() {
        _canCheckBiometric = canCheckBiometric;
      });
    } on PlatformException catch (e) {
      print(e);
    }
  }

  void _getAvailableBiometrics() async {
    List<BiometricType> availableBiometric;

    try {
      availableBiometric = await auth.getAvailableBiometrics();
      if (!mounted) return;
      setState(() {
        _availableBiometrics = availableBiometric;
      });
    } on PlatformException catch (e) {
      print(e);
    }
  }

CodePudding user response:

You have a typo at _availableBiometric = availableBiometric. I think you wanted to put ; instead of .

The setState() method will notify the flutter framework that the internal state of an object has changed. It is used for refreshing the widgets when the state is changed.

  • Related