khoren93 / flutter_zxing

Flutter plugin for scanning and generating QR codes using the ZXing library, supporting Android, iOS, and desktop platforms
https://pub.dev/packages/flutter_zxing
MIT License
89 stars 50 forks source link

onScan callback of ReaderWidget is not called on older Android devices #69

Open kaboc opened 1 year ago

kaboc commented 1 year ago

I've experienced the issue on Android 5.1, but not on Android 10. I haven't tried on other Android versions.

Environment:

Reproducible code:

android/app/src/main/AndroidManifest.xml

<!-- Add AdMob app ID -->
<!-- e.g. ca-app-pub-3940256099942544~3347511713 -->
<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>

lib/main.dart

import 'package:flutter/material.dart';

import 'package:flutter_zxing/flutter_zxing.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  MobileAds.instance.initialize();

  runApp(const App());
}

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  late final _ad = BannerAd(
    adUnitId: 'ca-app-pub-3940256099942544/6300978111', // Demo banner ad unit ID
    size: AdSize.banner,
    request: const AdRequest(),
    listener: const BannerAdListener(),
  )..load();

  @override
  void dispose() {
    _ad.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.black,
        body: Column(
          children: [
            Expanded(
              child: ReaderWidget(
                onScan: (code) {
                  print('text: ${code.text}'); // This is never called.
                },
              ),
            ),
            SizedBox(
              height: 50.0,
              child: AdWidget(ad: _ad),
            ),
          ],
        ),
      ),
    );
  }
}
kaboc commented 1 year ago

I run the code again and found the issue wasn't reproduced. I'm sorry.

It happens on an actual app, so there may be some differences. I'll compare the two and try to make a literally "reproducible" code. If I can't, I will just close this page then.

kaboc commented 1 year ago

I managed to reproduce the issue. It appears to have something to do with google_mobile_ads.

The example app repeatedly outputs the following message:

type 'ArgumentError' is not a subtype of type 'FutureOr\<Code>'