1

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
  • In general, something like a riverpod provider would be preferred to a classic Singleton pattern, because then you can get change notifications. Commented Jun 17, 2022 at 17:18

1 Answer 1

2

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);
//..............
//.......
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think you have to adjust that with positional parameters and so one. Hope this helps you!!
So, how can I assign the parameter values? Inside the constructor body or with this
It is not the best approach to use Singleton classes where there are class members that have to be variables, that changes time to time in different scopes. Consider a Singleton as Frizzed Class where if you have to change its members you can do it by passing by the single instance you've created. However in the constructor I've posted (without body) you can add the body then assign there the values of inner members. take in mind those values have to be valued only one time by the constructor, the second time you have to pass as I've explained up :)
However, depends in scenarios. There are scenarios, as utils classes etc etc. where Singleton classes are very USEFUL. Hoping that is your case :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.