【问题标题】:Failing Push Notification - Provider Bloc - FormatException推送通知失败 - Provider Bloc - FormatException
【发布时间】:2021-08-18 21:02:08
【问题描述】:

我已使用 Firebase 设置推送通知。

发生故障,错误消息不清楚

FormatException:输入意外结束(在字符 1 处) 强文本

我使用 Bloc

class PushNotificationsBloc
    extends Bloc<PushNotificationsEvent, PushNotificationsState> {
  final UserDataRepository userDataRepository;

  PushNotificationsBloc({this.userDataRepository})
      : super(PushNotificationsInitial());

  @override
  Stream<PushNotificationsState> mapEventToState(
    PushNotificationsEvent event,
  ) async* {
    if (event is SendNewNotification) {
      yield* mapSendNewNotificationToState(event.map);
    }
  }

  Stream<PushNotificationsState> mapSendNewNotificationToState(Map map) async* {
    yield SendNewNotificationInProgressState();
    try {
      String res = await userDataRepository.sendNewNotification(map);
      if (res != null) {
        yield SendNewNotificationCompletedState(res);
      } else {
        yield SendNewNotificationFailedState();
      }
    } catch (e) {
      print(e);
      yield SendNewNotificationFailedState();
    }
  }
}

【问题讨论】:

    标签: flutter push-notification


    【解决方案1】:

    当您期望 A 但得到 B 时会显示此错误。然后颤振尝试将其转换为对象 A 但这是不可能的,因为您得到了其他东西。例如,如果我希望 JSON 看起来像这样:

    {"username": "userName", "password": "password"}
    

    我把它转换成一个对象:

    class User {
      String username;
      String password;
    }
    

    它工作正常。但是如果出现问题并抛出异常,例如:

    {"Exception": "Internal Server Error"}
    

    然后它会显示你的错误,因为如果它试图将异常 JSON 转换为用户,它会失败。

    【讨论】:

    • 我只有推送通知的问题。 Firebase 连接的其余部分工作正常
    猜你喜欢
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2014-04-04
    • 1970-01-01
    • 2011-01-28
    相关资源
    最近更新 更多