1

I'm in a Flutter project using Getx. Every time I enter the screen that lists the records I get an error message as you can see below;

enter image description here

I don't know where I'm going wrong, but I'll leave the main parts of the code. I need to find where I'm going wrong.

Class Repository

 Future<List<Post>> getAlbum({

        bool isFavoritedPage = false,
        bool isNewEdition = false,
      }) async {
          dio.options.headers['Cookie'] = 'ASP.NET_SessionId=${user.sessionID}';

    final response = await dio.get(
      isFavoritedPage ? AppConstants.apiFavoritedsPost : AppConstants.apiPosts,
      queryParameters: {
        'sessionId': user.sessionID,
        'CodUserProfile': '${user.codUser!}',
        'CodUserLogged': '${user.codUser!}',
        'Page': '${page}',
        'pagesize': '10',
        'myPostOnly': isFavoritedPage ? 'true' : 'false',
      },
    );

    final body = response.data['ListPosts'] as List;
    return body.map((post) => Post.fromJson(post)).toList();
  }

Class Controller

var lstPost = List<Post>.empty(growable: true).obs;
  var page = 1;
  var isDataProcessing = false.obs;

  // For Pagination
  ScrollController scrollController = ScrollController();
  var isMoreDataAvailable = true.obs;


  @override
  void onInit() async {
    super.onInit();
    // Fetch Data
    getPost(page);
    //For Pagination
    paginateTask();

  }

  void getPost(var page) {
    try {
      isMoreDataAvailable(false);
      isDataProcessing(true);
      getAlbum(page).then((resp) {
        isDataProcessing(false);
        lstPost.addAll(resp);
      }, onError: (err) {
        isDataProcessing(false);
        showSnackBar("Error", err.toString(), Colors.red);
      });
    } catch (exception) {
      isDataProcessing(false);
      showSnackBar("Exception", exception.toString(), Colors.red);
    }
  }


  showSnackBar(String title, String message, Color backgroundColor) {
    Get.snackbar(title, message,
        snackPosition: SnackPosition.BOTTOM,
        backgroundColor: backgroundColor,
        colorText: Colors.white);
  }

  void paginateTask() {
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        print("reached end");
        page++;
        getMoreTask(page);
      }
    });
  }

  void getMoreTask(var page) {
    try {
      getAlbum(page).then((resp) {
        if (resp.length > 0) {
          isMoreDataAvailable(true);
        } else {
          isMoreDataAvailable(false);
          showSnackBar("Message", "Não existem registro", Colors.lightBlueAccent);
        }
        lstPost.addAll(resp);
      }, onError: (err) {
        isMoreDataAvailable(false);
        showSnackBar("Error", err.toString(), Colors.red);
      });
    } catch (exception) {
      isMoreDataAvailable(false);
      showSnackBar("Exception", exception.toString(), Colors.red);
    }
  }


  @override
  void onClose() {
    searchDrawerEC.dispose();
    super.onClose();
  }

  Future<List<Post>> getAlbum(pagina,[bool isFavoritedPage = false]) async {
    final response =
        await repository.getAlbum(isFavoritedPage: isFavoritedPage);
    return response;
  }

Class Page

Expanded(
         child: ListView.builder(
             itemBuilder: (BuildContext context, int index) {
               if (index == controller.lstPost.length - 1 &&
                   controller.isMoreDataAvailable.value == true) {
                 return Center(child: CircularProgressIndicator());
               }
               return PostWidget(post: controller.lstPost[index]);
             }
         ),
      ),

I'm basing myself on this github project.

https://github.com/RipplesCode/FlutterGetXTodoAppWithLaravel/tree/master/lib/app/modules/home

1 Answer 1

2

I don't use getx, but I see something odd in your Listview.builder. It feels as if you're abusing it a little, to also show the "no data" case, and there's also no count. I think it should have a count, so something like this:

if (lstPost.isEmpty) {
  return Center(child: CircularProgressIndicator());
} else {
return ListView.builder(
 itemCount: lstPost.length,
 itemBuilder: (BuildContext context, int index) {
    return PostWidget(...);
 }
 );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.