1

It is realy simple thing but I am unable to find the reason of issue.I have 2 stateful widget A and B, in widget A's init function I am calling the netwok calls and getting api response and rendering the latest data.In B, I fill the form and pushing to the server and after successfully posting the data to server I recall the Widget A to render latest data.

 Navigator.pushNamed(context, 'A');

Now Issue is every thing is working fine but after the data updating to server when i move the the widetA, my network calls return with null .

Cases in which it works. 1) if I don't call sendingPetPost function in widget B , and Move from B to A, then it works fine.

2) from Postman api works fine , no issue of calliing multiple times , i got the response each times.

3) there is no error of null instance of http call, issue is just response is null init function's code of A

  if(await utiles.isNetworkAvailbale())
       {
        model.getPostList(10, 1, 1, "", "token");
        model.getNewsList(10, 1, 1, "", "token");
        model.getEventsList(10, 1, 1, "", "token");
        model.getPetsList(35, 0, Provider.of<User>(context).id.toInt(), "token");
        print("====================================================");
       }else{
         isNetworkAvailable=false;
       }

Moving from Widget B to A after updating the data.

                                    sendingPetPost(
                                            endPOINT,
                                            widget.petDataImg,
                                            widget.isImgAdded)
                                        .whenComplete(() {
                                      Navigator.pushNamed(context, 'homeview');
                                    });

API call

  Future<List<Post>> getPostList(
      int pageSize, int pageNo, int cateId, String search, token) async {
    var posts = List<Post>();
    var response;
    print('$ENDPOINT/List?Type=blogs&PageNo=$pageNo&PageSize=$pageSize&Search');

    try {
      response = await client.get(
          //'$ENDPOINT/Data?PageSize=$pageSize&PageNo=$pageNo&nCategorieID=$cateId&Search=$search',
          '$ENDPOINT/List?Type=Blogs&PageNo=$pageNo&PageSize=$pageSize&Search',
          headers: headers);
    } catch (e) {
      print("error" + e.toString());
    }
    if (response.statusCode == 200) {                   
      // parse into List
      print("post =>> data received");
      var parsed = json.decode(response.body) as List<dynamic>;

      // loop and convert each item to Post
      for (var post in parsed) {
        posts.add(Post.fromJson(post));
      }
      return posts;
    }
    return null;
  }

Error

errorNoSuchMethodError: The getter 'codeUnits' was called on null.
I/flutter ( 7630): Receiver: null
I/flutter ( 7630): Tried calling: codeUnits
E/flutter ( 7630): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'statusCode' was called on null.
E/flutter ( 7630): Receiver: null
E/flutter ( 7630): Tried calling: statusCode

Update

it is so strange that I put headers inside the function and now it works,

  Future<String> sendingPetPost(
      String _endpoint, Map<String, dynamic> map, bool isIm) async {
/// if we make _headers gloable it create exception (same exception which is described above), but if we put _headers inside the fucntion like this, then it works.
           Map<String, String> _headers = {
      'content-type': 'application/json',
      'accept': 'application/json',
      'authorization':
          'Basic ' + base64Encode(utf8.encode('Sau***:Sa*****'))
    };
    Dio dio = Dio();
    try {
        FormData formData = FormData.fromMap({
          "pet_image": await MultipartFile.fromFile(
            map['path'],
            filename: map['name'],
          )
        });
     Response response = await dio.post("$ENDPOINT/AddPet?$_endpoint",
            data: formData, options: Options(headers: _headers));
       return json.decode(response.data)['message'];
    } on SocketException {
      throw Failure(UIHelper.NO_INTERNET_EXCEP);
    }
  }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.