无法在“ServicesBinding?”上访问属性“defaultBinaryMessenger”因为它可能为空

时间:2021-06-28 11:32:11

标签: flutter

我正在尝试将我的 Flutter 应用程序与 cloud_firestore 连接起来。但是当我运行代码时,它给出了与服务绑定相关的错误。为了克服这个问题,我使用了 WidgetsFlutterBinding.ensureInitialized()。使用后,同样出现以下错误。

Error: Property 'defaultBinaryMessenger' cannot be accessed on 'ServicesBinding?' because it is potentially null.
 - 'ServicesBinding' is from 'package:flutter/src/services/binding.dart' ('/C:/src/flutter/packages/flutter/lib/src/services/binding.dart').
Try accessing using ?. instead.
      _binaryMessenger ?? ServicesBinding.instance.defaultBinaryMessenger;

下面是我的代码:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Kindacode.com',
      home: HomePage(),
    );
  }
}

以下是我正在使用的版本:

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  firebase_core: ^1.3.0
  cloud_firestore: ^2.2.2

0 个答案:

没有答案