okadan / flutter-nfc-manager

A Flutter plugin for accessing the NFC features on Android and iOS.
https://pub.dev/packages/nfc_manager
MIT License
190 stars 125 forks source link

[ERROR:flutter/shell/common/shell.cc(1015)] #168

Open AnthonyGhizzo opened 6 months ago

AnthonyGhizzo commented 6 months ago

[ERROR:flutter/shell/common/shell.cc(1015)] The 'plugins.flutter.io/nfc_manager' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel. See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

[✓] Flutter (Channel stable, 3.16.3, on macOS 14.1.1 23B81 darwin-arm64, locale en-FR) • Flutter version 3.16.3 on channel stable at /opt/homebrew/Caskroom/flutter/3.7.11/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision b0366e0a3f (5 days ago), 2023-12-05 19:46:39 -0800 • Engine revision 54a7145303 • Dart version 3.2.3 • DevTools version 2.28.4

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2) • Android SDK at /Users/antomur/Library/Android/sdk • Platform android-33, build-tools 33.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15A507 • CocoaPods version 1.14.3

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.85.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.78.0

[✓] Connected device (3 available) • iPhone de X • iOS 17.1.2 21B101 • macOS (desktop) • macos • darwin-arm64 • macOS 14.1.1 23B81 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.71

[✓] Network resources • All expected network resources are available.

Code:

import 'package:flutter/material.dart';
import 'package:nfc_manager/nfc_manager.dart';

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

  @override
  _NFCScannerComponentState createState() => _NFCScannerComponentState();
}

class _NFCScannerComponentState extends State<NFCScannerComponent> {
  ValueNotifier<dynamic> result = ValueNotifier(null);
  bool _nfcAvailable = false;

  @override
  void initState() {
    super.initState();
    _checkNfcAvailability();
  }

  Future<void> _checkNfcAvailability() async {
    bool isAvailable = await NfcManager.instance.isAvailable();
    setState(() {
      _nfcAvailable = isAvailable;
    });
  }

  Future<void> _startNfcSession() async {
    NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async {
      result.value = tag.data;
      NfcManager.instance.stopSession();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          if (_nfcAvailable)
            ElevatedButton(
              onPressed: _startNfcSession,
              child: const Text('Démarrer la lecture NFC'),
            )
          else
            const Text('NFC non disponible sur cet appareil'),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: ValueListenableBuilder<dynamic>(
              valueListenable: result,
              builder: (context, value, _) => Text('${value ?? ''}'),
            ),
          ),
        ],
      ),
    );
  }
}
FritzMatthaeus commented 5 months ago

I've created a Pull Request to fix this issue.