[Solved] flutter Project Error: ScrollController attached to multiple scroll views, Failed assertion: line 109 pos 12

1. Explain

Recently, I encountered an error when writing the shuttle project:

The following assertion was thrown while handling a gesture:
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 109 pos 12: '_positions.length == 1'

Reasons for error reporting:
1 The drawing function added to the project needs to navigate to different pages through the menu items of the drawing
2. A bottom navigation bar is added to the home page to navigate to different pages through the pagecontroller
3. The project manages routing and status through geTx.

Description of the problem: navigate to the settings or other pages through the drawer, and then re-navigate to the home page through the drawer. Click the item item item of the BottomNavigationBar, and will appear the error messages:

Failed assertion: line 109 pos 12: ‘_positions.length == 1’


2. Solution:

1. Drawer navigation uses Get.offAndToNamed(‘12313’); to jump.
2. You need to reinitialize the pageController every time you jump to the homepage. Through the build method of GetView, the controller is reinitialized every time the homepage interface is redrawn.

Specific operation:

code for the navigation part of the drawer:

class AppRouteProvide {
  Future onRouteTo(String routeName) {
    switch (routeName) {
      case Routes.Auth:
        Get.toNamed(Routes.Auth);
        break;
      case Routes.Home:
        Get.offAndToNamed(Routes.Home);
        break;
      case Routes.Help:
        Get.offAndToNamed(Routes.Help);
        break;
      case Routes.Splash:
        Get.toNamed(Routes.Splash);
        break;
      case Routes.Connect:
        Get.offAndToNamed(Routes.Connect);
        break;
      case Routes.Setting:
        Get.offAndToNamed(Routes.Setting);
        break;
    }
    return Future.value();
  }
}

Code of home page view:

class HomeScreen extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    // print('HomeScreen build');
    if (controller.pageController.hasClients) {
      controller.onClose();
      controller.onInit();
    }
    return AppbarWidgetScreen(
      body: _buildBody(context),
      bottomBar: _buildBottomNavigationBar(context),
    );
  }

Home page controller part code


class HomeController extends GetxController {
  final ApiRepository apiRepository;
  RxInt selectedIndex = 0.obs;

  late PageController pageController;
  RxList<Widget> widgetPage = <Widget>[
    HomeMainScreen(),
    HomeBuyScreen(),
    HomeCertScreen(),
    HomeSellScreen(),
    HomeMineScreen(),
  ].obs;

  HomeController({required this.apiRepository});

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onInit() {
    print('HomeController onInit()');
    pageController = PageController(
      initialPage: selectedIndex.value,
      keepPage: true,
    );
    super.onInit();
  }

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

Read More: