简体   繁体   English

ScrollController 附加到多个滚动视图。 flutter

[英]ScrollController attached to multiple scroll views. flutter

I try to rebuild instagram stories by using GetX.我尝试使用 GetX 重建 Instagram 故事。 I always receive this issue.我总是收到这个问题。 Can anyone help me solve this problem?谁能帮我解决这个问题?

ScrollController attached to multiple scroll views. ScrollController 附加到多个滚动视图。 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 109 pos 12: '_positions.length == 1' 'package:flutter/src/widgets/scroll_controller.dart':断言失败:第 109 行 pos 12:'_positions.length == 1'

I try to rebuild instagram stories by using GetX.我尝试使用 GetX 重建 Instagram 故事。 I always receive this issue.我总是收到这个问题。 Can anyone help me solve this problem?谁能帮我解决这个问题?

ScrollController attached to multiple scroll views. ScrollController 附加到多个滚动视图。 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 109 pos 12: '_positions.length == 1' 'package:flutter/src/widgets/scroll_controller.dart':断言失败:第 109 行 pos 12:'_positions.length == 1'

import 'package:flamingo/Business_Logic/GetXControllers/Pages_Controllers/Stories_Controller.dart';
import 'package:flamingo/Data/DataProviders/StoriesList.dart';
import 'package:flamingo/Data/Models/All_Models.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class Stories extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<StoriesController>(
      init: StoriesController(),
      builder: (storiesCtrl) {
        return Scaffold(
          backgroundColor: Colors.black,
          body: GestureDetector(
            onTapDown: (details) => storiesCtrl.onTapDown(
                details, story[storiesCtrl.currentIndex.value]),
            child: Stack(
              children: <Widget>[
                PageView.builder(
                  controller: storiesCtrl.pageC,
                  physics: NeverScrollableScrollPhysics(),
                  itemBuilder: (context, i) {
                    final StoryModel s = story[i];
                    switch (s.media) {
                      case MediaType.image:
                        return Image.network(
                          s.image,
                          fit: BoxFit.cover,
                        );
                    }
                    return const SizedBox.shrink();
                  },
                ),
                Positioned(
                  top: 15.0,
                  left: 10.0,
                  right: 10.0,
                  child: Row(
                    children: story
                        .asMap()
                        .map((i, e) {
                          return MapEntry(
                              i,
                              Flexible(
                                child: Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 1.5),
                                  child: LayoutBuilder(
                                      builder: (context, constraints) {
                                    return Stack(
                                      children: <Widget>[
                                        _buildContainer(
                                          double.infinity,
                                          i < storiesCtrl.currentIndex.value
                                              ? Colors.white
                                              : Colors.white.withOpacity(0.5),
                                        ),
                                        i == storiesCtrl.currentIndex.value
                                            ? AnimatedBuilder(
                                                animation: storiesCtrl.animC,
                                                builder: (context, child) {
                                                  return _buildContainer(
                                                    constraints.maxWidth *
                                                        storiesCtrl.animC.value,
                                                    Colors.white,
                                                  );
                                                },
                                              )
                                            : const SizedBox.shrink(),
                                      ],
                                    );
                                  }),
                                ),
                              ));
                        })
                        .values
                        .toList(),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Container _buildContainer(double width, Color color) {
    return Container(
      height: 5.0,
      width: width,
      decoration: BoxDecoration(
        color: color,
        border: Border.all(
          color: Colors.black26,
          width: 0.8,
        ),
        borderRadius: BorderRadius.circular(3.0),
      ),
    );
  }
}


import 'package:flamingo/Data/Models/StoryModel.dart';
import 'package:flamingo/Utils/AllUtils.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:flamingo/Data/DataProviders/StoriesList.dart';
import 'package:get/get.dart';

class StoriesController extends GetxController
    with SingleGetTickerProviderMixin {
  List<StoryModel> stories;
  AnimationController animC;
  var pageC;
  var currentIndex = 0.obs;

  @override
  void onInit() {
    stories = story;
    super.onInit();
    pageC = PageController();
    animC = AnimationController(vsync: this);
    final StoryModel firstStory = stories.first;
    loadStory(story: firstStory, animateToPage: false);

    animC.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        animC.stop();
        animC.reset();

        if (currentIndex.value + 1 < stories.length) {
          currentIndex.value += 1;
          loadStory(story: stories[currentIndex.value]);
          update();
        } else {
          currentIndex.value = 0;
          loadStory(story: stories[currentIndex.value]);
          update();
        }
      }
    });
  }

  void onTapDown(TapDownDetails details, StoryModel s) {
    final double dx = details.globalPosition.dx;
    if (dx < GlobalSize.screenWidth / 3) {
      if (currentIndex.value - 1 >= 0) {
        currentIndex.value -= 1;
        loadStory(story: story[currentIndex.value]);
        update();
      }
    } else if (dx > 2 * GlobalSize.screenWidth / 3) {
      if (currentIndex.value + 1 < story.length) {
        currentIndex.value += 1;
        loadStory(story: story[currentIndex.value]);
      } else {
        currentIndex.value = 0;
        loadStory(story: story[currentIndex.value]);
      }
    }
  }

  void loadStory({StoryModel story, bool animateToPage = true}) {
    animC.stop();
    animC.reset();

    switch (story.media) {
      case MediaType.image:
        animC.duration = story.duration;
        animC.forward();
        break;
    }
    if (animateToPage) {
      pageC.animateToPage(
        currentIndex.value,
        duration: const Duration(microseconds: 1),
        curve: Curves.easeInOut,
      );
    }
  }

  @override
  void onClose() {
    pageC.value.dispose();
    animC.dispose();
    super.onClose();
  }
}

Okay so I had this same issue but for me it was not actually related to the code where I used my controller.好的,所以我遇到了同样的问题,但对我来说,它实际上与我使用 controller 的代码无关。 Typically this issue shows up if you access the same controller from multiple pages.如果您从多个页面访问相同的 controller,通常会出现此问题。 However, it also occurs if you have multiple instances of the same page on the router stack.但是,如果您在路由器堆栈上有同一页面的多个实例,也会发生这种情况。 In my case I was using Get.to() elsewhere in my application to navigate back to my home page which created another instance of my home page on the stack, hence the multiple controllers error.在我的情况下,我在应用程序的其他地方使用Get.to()导航回我的主页,这在堆栈上创建了我的主页的另一个实例,因此出现了多个控制器错误。

So to solve it, I changed Get.to() to Get.offAll() .所以为了解决它,我将Get.to()更改为Get.offAll() If you are using named routes, switch Get.toNamed() to Get.offAllNamed() .如果您使用命名路由,请将Get.toNamed()切换为Get.offAllNamed()

If you get errors after doing this that say your controllers are no longer found, in your bindings, set permanent: true when using Get.put() to instantiate your controller: Get.put(Home(), permanent: true);如果在执行此操作后出现错误,表明不再找到您的控制器,请在绑定中设置permanent: true在使用Get.put()实例化 controller 时设置为 true: Get.put(Home(), permanent: true);

Looking at your code, it does not look like the error is coming from using the same controller twice and may instead occur when you route back to this page from somewhere in your application.查看您的代码,错误似乎不是来自使用相同的 controller 两次,而是当您从应用程序中的某个位置路由回此页面时可能会发生错误。 Took me forever to figure out but essentially Flutter makes another instance of the "shared" controller when you add the same page onto the stack so you have to remove the original page from the stack when navigating.花了我很长时间才弄清楚,但本质上 Flutter 在您将相同的页面添加到堆栈中时会创建另一个“共享”controller 实例,因此您必须在导航时从堆栈中删除原始页面。

If anyone gets this error from navigating and is not using GetX for state management, use Navigation.pushNamedAndRemoveUntil() to navigate back to the page with the controller.如果有人从导航中收到此错误并且没有使用 GetX 进行 state 管理,请使用Navigation.pushNamedAndRemoveUntil()导航回包含 controller 的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误:“ScrollController 未附加到任何滚动视图。” 在卷轴上 - Flutter error: 'ScrollController not attached to any scroll views.' on scroll Flutter - PageController “ScrollController 未附加到任何滚动视图。” - Flutter - PageController " ScrollController not attached to any scroll views." "Flutter ScrollController 附加到多个滚动视图" - flutter ScrollController attached to multiple scroll views Flutter 异常:附加到多个滚动视图的 ScrollController - Flutter Exception: ScrollController attached to multiple scroll views ScrollController 附加到多个滚动视图 - ScrollController attached to multiple scroll views 如何使用 2 个可重新排序的行? - 例外:“ScrollController 附加到多个滚动视图。” - How can I use 2 reorderble rows ? - Exception: "ScrollController attached to multiple scroll views." Flutter - ScrollController 未附加到任何滚动视图 - Flutter - ScrollController not attached to any scroll views PageCotroller 错误 ScrollController 附加到多个滚动视图 - PageCotroller error ScrollController attached to multiple scroll views ScrollController 未附加到任何滚动视图 - ScrollController not attached to any scroll views 我将 ScrollController 附加到多个滚动视图 - I'm getting ScrollController attached to multiple scroll views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM