flutter riverpod _CastError (Null check operator used on a null value)

Viewed 32

I am developing an application with flutter and flutter_riverpod. I am getting datas from mysql database with php and I am getting null error. I know there are many answer for this question on the stackoverflow but they are not about riverpod. Therefore I ask here.

I'm encrypting my datas before send to database. And I am decrypting the data when I show on listview. But I am getting null error when I show on listview text on the widget tree. There is no error on the encrypt and send data. but there is error on the decrypt.

Note: I am gettin error only restart mode but I don't getting error hot reload mode after get the datas. I am getting normal encrypted data and after I am changing my code. I click hot reload and decrypt data is okey, no problem. But when I restart I am gettig error.

This my code for AES encrypt and decrypt.

 import 'package:encrypt/encrypt.dart';

class EncryptData{
//for AES Algorithms

  static Encrypted? encrypted;
  static var decrypted;


 static encryptAES(plainText){
   final key = Key.fromUtf8('my 32 length key................');
   final iv = IV.fromLength(16);
   final encrypter = Encrypter(AES(key));
    encrypted = encrypter.encrypt(plainText, iv: iv);
   return(encrypted!.base64);
 }

  static decryptAES(plainText){
    final key = Key.fromUtf8('my 32 length key................');
    final iv = IV.fromLength(16);
    final encrypter = Encrypter(AES(key));
    decrypted = encrypter.decrypt(encrypted!, iv: iv);
    return(decrypted);
  }
}

this is my code to show data on lisview. You can see the decrypt on the text in listview.

    class PersonsListW extends ConsumerWidget {
  const PersonsListW({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final futureCatFacts = ref.watch(getPersonsFProvider);
    return Expanded(
      child: futureCatFacts.when(
        loading: () => const ShimmerPersonsW(),
        error: (err, stack) => Text('Error: $err'),
        data: (data) {
          final decodedData = json.decode(data.body);
          return ListView.separated(
            separatorBuilder: (BuildContext context, int index) {
              if (index % 3 == 0) {
                return const Divider();
              }
              return const Divider();
            },
            shrinkWrap: true,
            itemCount: decodedData.length,
            physics: const BouncingScrollPhysics(
                parent: AlwaysScrollableScrollPhysics()),
            itemBuilder: (BuildContext context, int index) {
              return Padding(
                padding: const EdgeInsets.only(
                ),
                child: InkWell(
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ClipRRect(
                          borderRadius: BorderRadius.circular(10),
                          child: Image.network(
                            decodedData[index]['personPhoto'].toString(),
                            fit: BoxFit.cover,
                            height: 70,
                            width: 70,
                          )),
                      const SizedBox(
                        width: 10,
                      ),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                          index==0
                          ?EncryptData.decryptAES(decodedData[index]['personName'])
                          :decodedData[index]['personName'],
                          style: const TextStyle(
                              fontSize: 15, fontWeight: FontWeight.bold),
                        ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}
1 Answers

Change this:

static decryptAES(plainText){
    final key = Key.fromUtf8('my 32 length key................');
    final iv = IV.fromLength(16);
    final encrypter = Encrypter(AES(key));
    decrypted = encrypter.decrypt(encrypted!, iv: iv);
    return(decrypted);
  }

to :

static decryptAES(plainText){
    final key = Key.fromUtf8('my 32 length key................');
    final iv = IV.fromLength(16);
    final encrypter = Encrypter(AES(key));
    encrypted = encrypter.encrypt(plainText, iv: iv);
    if(encrypted != null){
      decrypted = encrypter.decrypt(encrypted!, iv: iv);
    }
    
    return(decrypted);
  }

then :

Text(
   index==0
   ?EncryptData.decryptAES(decodedData[index]['personName']??'ronaldo')
        :decodedData[index]['personName'] ?? 'ronaldo',
   style: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
 ),
Related