如何将Singleton应用于课程?
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当代码和消息显示您缺少构造函数时。
代码必须如下:
As code and message shows you are missing the constructor.
The code have to be as follows: