如何将Singleton应用于课程?

发布于 2025-01-31 20:47:19 字数 3921 浏览 2 评论 0原文

在Flutter中,我试图使下面的类A singleton

class ChatMessagesListView extends StatefulWidget {
  /// Creates a chat list widget
  ChatMessagesListView(
      {Key? key,
      this.isLastPage,
      required this.itemBuilder,
      required this.items,
      this.onEndReached,
      this.onEndReachedThreshold,
      required this.tempMessageIndicators})
      : super(key: key);

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// When true, indicates that there are no more pages to load and
  /// pagination will not be triggered.
  final bool? isLastPage;

  final List<TempMessageIndicator> tempMessageIndicators;

  /// Items to build
  final List<Object> items;

  /// Item builder
  final Widget Function(Object, int? index) itemBuilder;

  /// Used for pagination (infinite scroll). Called when user scrolls
  /// to the very end of the list (minus [onEndReachedThreshold]).
  final Future<void> Function()? onEndReached;

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// Can be anything from 0 to 1, where 0 is immediate load of the next page
  /// as soon as scroll starts, and 1 is load of the next page only if scrolled
  /// to the very end of the list. Default value is 0.75, e.g. start loading
  /// next page when scrolled through about 3/4 of the available content.
  final double? onEndReachedThreshold;

  /// Scroll controller to control the scrolling
  final ScrollController scrollController = ScrollController();
  final ScrollController scrollController2 = ScrollController();

  @override
  _ChatMessagesListViewState createState() => _ChatMessagesListViewState();
}

这是我尝试的方法

class ChatMessagesListView extends StatefulWidget {
  static ChatMessagesListView? _instance;

  factory ChatMessagesListView(
          {Key? key,
    isLastPage,
    required itemBuilder,
    required items,
    onEndReached,
    onEndReachedThreshold,
    required tempMessageIndicators,}) =>
      _instance ??=
          ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// When true, indicates that there are no more pages to load and
  /// pagination will not be triggered.
  late final bool? isLastPage;

  late final List<TempMessageIndicator> tempMessageIndicators;

  /// Items to build
  late final List<Object> items;

  /// Item builder
  late final Widget Function(Object, int? index) itemBuilder;

  /// Used for pagination (infinite scroll). Called when user scrolls
  /// to the very end of the list (minus [onEndReachedThreshold]).
  late final Future<void> Function()? onEndReached;

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// Can be anything from 0 to 1, where 0 is immediate load of the next page
  /// as soon as scroll starts, and 1 is load of the next page only if scrolled
  /// to the very end of the list. Default value is 0.75, e.g. start loading
  /// next page when scrolled through about 3/4 of the available content.
  late final double? onEndReachedThreshold;

  /// Scroll controller to control the scrolling
  final ScrollController scrollController = ScrollController();
  final ScrollController scrollController2 = ScrollController();

  @override
  _ChatMessagesListViewState createState() => _ChatMessagesListViewState();
}

,但是它给我带来了一个错误,该方法'_'并未定义为'chatmessageslistview'.try将名称纠正到现有方法的名称,或定义名为<<的方法< /代码>。因此,基本上,我无法执行以下行

_instance ??=
              ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);

加,如果您查看我的第一组代码,您会看到我将构造函数数据分配给变量。我相信这也不会在这里发生。

如何使此类A Singleton

In flutter, i am trying to make the below class a Singleton.

class ChatMessagesListView extends StatefulWidget {
  /// Creates a chat list widget
  ChatMessagesListView(
      {Key? key,
      this.isLastPage,
      required this.itemBuilder,
      required this.items,
      this.onEndReached,
      this.onEndReachedThreshold,
      required this.tempMessageIndicators})
      : super(key: key);

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// When true, indicates that there are no more pages to load and
  /// pagination will not be triggered.
  final bool? isLastPage;

  final List<TempMessageIndicator> tempMessageIndicators;

  /// Items to build
  final List<Object> items;

  /// Item builder
  final Widget Function(Object, int? index) itemBuilder;

  /// Used for pagination (infinite scroll). Called when user scrolls
  /// to the very end of the list (minus [onEndReachedThreshold]).
  final Future<void> Function()? onEndReached;

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// Can be anything from 0 to 1, where 0 is immediate load of the next page
  /// as soon as scroll starts, and 1 is load of the next page only if scrolled
  /// to the very end of the list. Default value is 0.75, e.g. start loading
  /// next page when scrolled through about 3/4 of the available content.
  final double? onEndReachedThreshold;

  /// Scroll controller to control the scrolling
  final ScrollController scrollController = ScrollController();
  final ScrollController scrollController2 = ScrollController();

  @override
  _ChatMessagesListViewState createState() => _ChatMessagesListViewState();
}

This is what I tried

class ChatMessagesListView extends StatefulWidget {
  static ChatMessagesListView? _instance;

  factory ChatMessagesListView(
          {Key? key,
    isLastPage,
    required itemBuilder,
    required items,
    onEndReached,
    onEndReachedThreshold,
    required tempMessageIndicators,}) =>
      _instance ??=
          ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// When true, indicates that there are no more pages to load and
  /// pagination will not be triggered.
  late final bool? isLastPage;

  late final List<TempMessageIndicator> tempMessageIndicators;

  /// Items to build
  late final List<Object> items;

  /// Item builder
  late final Widget Function(Object, int? index) itemBuilder;

  /// Used for pagination (infinite scroll). Called when user scrolls
  /// to the very end of the list (minus [onEndReachedThreshold]).
  late final Future<void> Function()? onEndReached;

  /// Used for pagination (infinite scroll) together with [onEndReached].
  /// Can be anything from 0 to 1, where 0 is immediate load of the next page
  /// as soon as scroll starts, and 1 is load of the next page only if scrolled
  /// to the very end of the list. Default value is 0.75, e.g. start loading
  /// next page when scrolled through about 3/4 of the available content.
  late final double? onEndReachedThreshold;

  /// Scroll controller to control the scrolling
  final ScrollController scrollController = ScrollController();
  final ScrollController scrollController2 = ScrollController();

  @override
  _ChatMessagesListViewState createState() => _ChatMessagesListViewState();
}

However it is throwing me an error, The method '_' isn't defined for the type 'ChatMessagesListView'.Try correcting the name to the name of an existing method, or defining a method named. So basically, I can't execute the below line

_instance ??=
              ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);

Plus, if you look at my first set of code, you can see I am assigning the constructor data to variables. I am sure that is not happening here as well.

How can I make this class a Singleton?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2025-02-07 20:47:19

当代码和消息显示您缺少构造函数时。
代码必须如下:

class ChatMessagesListView extends StatefulWidget {
  ChatMessagesListView._(Key? key,
      isLastPage,
      required itemBuilder,
      required items,
      onEndReached,
      onEndReachedThreshold,
      required tempMessageIndicators);

  static ChatMessagesListView? _instance;

  factory ChatMessagesListView(
      {Key? key,
        isLastPage,
        required itemBuilder,
        required items,
        onEndReached,
        onEndReachedThreshold,
        required tempMessageIndicators,}) =>
      _instance ??=
          ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);
//..............
//.......
}

As code and message shows you are missing the constructor.
The code have to be as follows:

class ChatMessagesListView extends StatefulWidget {
  ChatMessagesListView._(Key? key,
      isLastPage,
      required itemBuilder,
      required items,
      onEndReached,
      onEndReachedThreshold,
      required tempMessageIndicators);

  static ChatMessagesListView? _instance;

  factory ChatMessagesListView(
      {Key? key,
        isLastPage,
        required itemBuilder,
        required items,
        onEndReached,
        onEndReachedThreshold,
        required tempMessageIndicators,}) =>
      _instance ??=
          ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);
//..............
//.......
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文