flutter 配置单元:未处理异常:类型“Null”不是类型强制转换中类型“String”的子类型

fcy6dtqo  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(221)

我正在做一个项目,我使用Hive数据库本地存储数据。它最初工作得很好。但当我关闭应用程序时,从后台应用程序中删除它,然后重新打开它抛出错误和应用程序崩溃。Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast
我不明白为什么会出现这个错误。
这是我的代码。

...

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  Hive.registerAdapter<ScheduleModel>(ScheduleModelAdapter());
  Hive.registerAdapter<AddressModel>(AddressModelAdapter());
  Hive.registerAdapter<ProductModel>(ProductModelAdapter());

  await Hive.initFlutter(); // Initialize Hive

  await Future.wait([
    Hive.openBox<ProductModel>('products'),
    Hive.openBox<ScheduleModel>('schedule'),
    Hive.openBox<AddressModel>('address'),
  ]);

// Check if the boxes are already open before opening them
  if (!Hive.isBoxOpen('products')) {
    await Hive.openBox<ProductModel>('products');
  }
  if (!Hive.isBoxOpen('schedule')) {
    await Hive.openBox<ScheduleModel>('schedule');
  }
  if (!Hive.isBoxOpen('address')) {
    await Hive.openBox<AddressModel>('address');
  }
  FirebaseMessaging messaging = FirebaseMessaging.instance;
  String? fcmToken = await messaging.getToken();
  await messaging.subscribeToTopic('DailyOrder');
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.setString("fcmToken", fcmToken.toString());

  runApp(const MyApp());
}
...//main function

地址模型

@HiveType(typeId: 2)
class AddressModel extends HiveObject {
  @HiveField(0)
  String addtype;
  @HiveField(1)
  String houseno;
  @HiveField(2)
  String buildingname;
  @HiveField(3)
  String area;
  @HiveField(4)
  String landmark;
  @HiveField(5)
  String city;
  @HiveField(6)
  String state;
  @HiveField(7)
  String latitude; // Add this field
  @HiveField(8)
  String longitude; // Add this field

  AddressModel({
    required this.addtype,
    required this.houseno,
    required this.buildingname,
    required this.area,
    required this.landmark,
    required this.city,
    required this.state,
    required this.latitude, // Initialize latitude
    required this.longitude, // Initialize longitude
  });
}

错误消息

anchor = null
E/flutter (26229): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'PlatformException' is not a subtype of type 'String?'
E/flutter (26229): #0      _FirstPageState._getAddressFromLatLng.<anonymous closure> (package:newmart/OTP/landing_page.dart:154:18)
E/flutter (26229): #1      _RootZone.runUnary (dart:async/zone.dart:1661:54)

代码132 - 160

// Latitude: 37.4219983, Longitude: -122.084
  Future<void> _getAddressFromLatLng(Position position) async {
    await placemarkFromCoordinates(
            _currentPosition!.latitude, _currentPosition!.longitude)
        .then((List<Placemark> placemarks) async {
      Placemark place = placemarks[0];
      city = place.locality.toString();
      statename = place.administrativeArea.toString();
      pincode = place.postalCode.toString();
      area = place.subLocality.toString();
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString("city", place.locality.toString());
      prefs.setString("pincode", place.postalCode.toString());
      prefs.setString("area", place.subLocality.toString());
      prefs.setString("statename", place.administrativeArea.toString());
      prefs.setString("country", place.country.toString());
      setState(() {
        // print(place.toString());
      });
    }).catchError((e) {
      debugPrint(e);
    });
  }
gzszwxb4

gzszwxb41#

检查某个地方你需要提供字符串但有null,只需记录值以检查哪个值提供null,并相应地处理它。

相关问题