Behnam
Behnam

Reputation: 329

PageCotroller error ScrollController attached to multiple scroll views

I have a class like this :

class MyController {
  static PageController globalController = PageController(initialPage: 0);

  void diispose() {
    globalController.dispose();
  }
}

I'd like to use this PageController in two different locations to modify thePageView however after use MyController.globalController.jumpToPage(1); I got this error :

ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
package:flutter/…/widgets/scroll_controller.dart:1
Failed assertion: line 108 pos 12: '_positions.length == 1'

Upvotes: 0

Views: 322

Answers (1)

Guilherme Gabanelli
Guilherme Gabanelli

Reputation: 566

I think you cannot use the same PageController for two scroll views, but you can create one for each scroll and one method that jump all scrolls to page, as the example below:

class MyController {
  static PageController globalControllerOne = PageController(initialPage: 0);
  static PageController globalControllerTwo = PageController(initialPage: 0);

  void jumpToPage(int page) {
    globalControllerOne.jumpToPage(page);
    globalControllerTwo.jumpToPage(page);
  }

  void dispose() {
    globalController.dispose();
  }
}

Upvotes: 1

Related Questions