How to apply Singleton to a class?

Viewed 103

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?

1 Answers

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);
//..............
//.......
}
Related