在扑动中,我试图使下面的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();
}
然而,它给了我一个错误,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
。所以基本上,我不能执行下面的
_instance ??=
ChatMessagesListView._(key, isLastPage, itemBuilder, items, onEndReached, onEndReachedThreshold, tempMessageIndicators);
另外,如果你看我的第一组代码,你可以看到我将构造函数数据分配给变量。我相信这里也不会发生这种情况。
如何使这个类成为Singleton
?
正如代码和消息所示,您缺少构造函数。代码必须如下所示:
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);
//..............
//.......
}