Search code examples
flutterdartqr-code

Errors on QrImage class/qr_flutter package


This is the screenshot of the errors

In the image, you would notice its mostly about the QrImage class for Dart/Flutter. I assumed there was a compatibility issue with the version of either Dart or Flutter, or maybe it involves the importing of 'package:qr_flutter/qr_flutter.dart'.

Here is the code:

import 'dart:convert';
import 'package:boilerplate/Services/Authentication/AuthService.dart';
import 'package:boilerplate/ViewModel/ProfileViewModels/ProfileViewModel.dart';
import 'package:boilerplate/ui/Event/event_widgets.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:crypto/crypto.dart';

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

  @override
  State<AgentQR> createState() => _AgentQRState();
}

class _AgentQRState extends State<AgentQR> {
  String hashedQr() {
    //hashes the date and time to create a unique qr code for each transaction
    var bytes1 = utf8.encode(
        "${DateTime.now().month}${DateTime.now().day}${DateTime.now().hour}"); // data being hashed
    var hashedDate = sha256.convert(bytes1);
    var detailsProvider = context.read<ProfileViewModel>();
    return "${AuthService().getUserID()}:${detailsProvider.firstName} ${detailsProvider.lastName}:${detailsProvider.location}:$hashedDate";
  }

  @override
  void initState() {
    super.initState();
    //isQrExist();
  }

    @override
    Widget build(BuildContext context) {
      return QrImage(
      data: hashedQr(),
      errorCorrectionLevel: QrErrorCorrectLevel.H,
      version: QrVersions.auto,
      backgroundColor: Colors.white,
      foregroundColor: Colors.black,
      gapless: true,
      padding: EdgeInsets.all(7),
      );
    }
 }


In my pubspec.yaml file, I tried changing the version of qr_flutter from ^4.0.0 to ^4.1.0 but nothing happened, the errors were still present. I also tried adding a dependency for qr_flutter:

 qr_flutter:
     git:
       url: https://github.com/theyakka/qr.flutter.git

nothing happened as well.

Flutter version: 3.7.12 Dart version: 2.19.6 qr_flutter version: ^4.1.0

I downgraded my flutter and dart to these versions because the mobile app project of the company requires a null-safety. I used this article as a guide: https://dart.dev/null-safety/migration-guide


Solution

  • There was a breaking change in 4.0.1

    https://pub.dev/packages/qr_flutter/changelog

    4.0.1 
    Bump qr dependency (from ^2.0.0 to ^3.0.0).
    BREAKING: Rename QrImage to QrImageView