簡體   English   中英

Riverpod 的 ConsumerWidget 在我的 flutter 項目中無法識別

[英]Riverpod's ConsumerWidget not recognized in my flutter project

我已經在我的 flutter 項目中安裝了 Riverpod,我正在嘗試創建一個擴展 ConsumerWidget 的 class,但后者未被我的 IDE 識別,盡管一切似乎都是正確的(Android Studio)並且它用紅色強調了它。

這是我的 class:

import 'package:flutter/material.dart';
import 'package:testproject/models/message_model.dart';
import 'package:riverpod/riverpod.dart';

class MessageList extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    List<Message> messages = ref.watch(todosProvider);
    return widget();
  }
}

這是我的 pubspec 文件:

name: testproject
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.1+2

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cool_alert: ^1.1.0
  cupertino_icons: ^1.0.3
  http: ^0.13.3
  flutter_spinkit: ^5.0.0
  url_launcher: ^6.0.9
  firebase_messaging: ^10.0.4
  firebase_core: ^1.4.0
  overlay_support: ^1.2.1
  path_provider: ^2.0.2
  lint: ^1.5.3
  riverpod: ^1.0.3

dev_dependencies:
  flutter_test:
    sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - images/notification_icon.png
  #   - images/a_dot_ham.jpeg
  fonts:
    - family: Pacifico
      fonts:
        - asset: fonts/Pacifico-Regular.ttf
    - family: Source Sans Pro
      fonts:
        - asset: fonts/SourceSansPro-Regular.ttf

不知道我是否錯過了什么或者我做錯了什么,但我無法找到它是什么。

由於您使用的是 Flutter,因此您應該使用 flutter_riverpod 或 hooks_riverpod(如果您使用的是鈎子)。 Riverpod 僅適用於 dart。 所以你只需要使用正確的 package。

在此處輸入圖像描述

來源: https://riverpod.dev/docs/getting_started/#what-package-to-install

package “riverpod” 用於 dart 程序,因此所有與 Flutter 相關的類都被剝離了。

嘗試改用flutter_riverpod ,我認為應該可以。

這里查看不同的 Riverpod 包。

你導入庫了嗎?

import 'package:flutter_riverpod/flutter_riverpod.dart';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM