Can't start Alice HTTP Inspector. Please add NavigatorKey to your application

1.3k Views Asked by At

navigatorKey has already been added to the code, still it shows error(red line) under the navigatorKey.

@override
Widget build(BuildContext context) {
return WillPopScope(
navigatorKey: _alice.getNavigatorKey(),
child: Theme(
data: ThemeData(
brightness: Brightness.light,
primarySwatch: AppColor.appColor,
primaryColor: AppColor.white,
buttonTheme: ButtonThemeData().copyWith(
buttonColor: AppColor.appColor,
textTheme: ButtonTextTheme.primary),
fontFamily: "EncodeSans",
),
child: layout()),

onWillPop: () {
return handleBackPress();
});
}
2

There are 2 best solutions below

3
Taleb On

change navigatorKey to key:

you code must look like belew:

@override
Widget build(BuildContext context) {
return WillPopScope(
key: _alice.getNavigatorKey(), // here i replace key with navigatorKey
child: Theme(
data: ThemeData(
brightness: Brightness.light,
primarySwatch: AppColor.appColor,
primaryColor: AppColor.white,
buttonTheme: ButtonThemeData().copyWith(
buttonColor: AppColor.appColor,
textTheme: ButtonTextTheme.primary),
fontFamily: "EncodeSans",
),
child: layout()),

onWillPop: () {
return handleBackPress();
});
}
0
Sanjay Sharma On

Use http package http: any version

  1. Create new class(APIConstant) and then use this code in the class
  static Alice alice=Alice(
    showShareButton: true,
    showNotification: true,
  );
  1. Use this code in main.dart

    navigatorKey: APIConstant.alice.getNavigatorKey(),

  2. Like this:

if (kDebugMode) APIConstant.alice.onHttpResponse(response, body: jsonEncode(sendOtpReq));

  Future<SendOtpModels?> sendOtpApi(SendOtpReq  sendOtpReq) async {
    try {
      CustomLoader.showLoader("Please wait");
      final response = await http.post(Uri.parse(APIConstant.sendOtp),
        headers: APIConstant.authHeader,
        body: jsonEncode(sendOtpReq),
      );
      if (kDebugMode) APIConstant.alice.onHttpResponse(response, body: jsonEncode(sendOtpReq));

      const utf8Decoder = Utf8Decoder(allowMalformed: true);
      final decodedBytes = utf8Decoder.convert(response.bodyBytes);
      Map<String, dynamic> responseBody = json.decode(decodedBytes);
      if (response.statusCode == 200) {
        CustomLoader.closeLoader();
        return SendOtpModels.fromJson(responseBody);
      }
    }
    on TimeoutException catch (e) {
      CustomLoader.closeLoader();
      CustomLoader.showToast(e.message.toString());
    }  catch (e) {
      CustomLoader.closeLoader();
      CustomLoader.showToast(e.toString());
    }
    return null;
  }