降级JDK/SDK后Flutter项目类型不匹配报错求助
问题分析
核心问题是同一个类被从两个不同路径导入,导致Dart将其识别为完全不同的类型——比如AppState/_1/来自package:格式路径,AppState/_2/来自本地绝对路径,两者被判定为不兼容类型,进而引发Reducer、中间件、Stream的类型匹配错误。
报错信息
/D://maghsala//maghsala_customer//lib//main.dart:49:11: Error: The argument type 'AppState/_1/ Function(AppState/_1/, dynamic)' can't be assigned to the parameter type 'AppState/_2/ Function(AppState/_2/, dynamic)'. - 'AppState/_1/' is from 'package:maghsala_customer/states/AppState.dart' ('lib/states/AppState.dart'). - 'AppState/_2/' is from '/D://maghsala//maghsala_customer//lib//states/AppState.dart'. appReducer, ^ /D://maghsala//maghsala_customer//lib//main.dart:51:15: Error: The argument type 'List<dynamic Function(Store<AppState/_1/>, dynamic, dynamic Function(dynamic))>' can't be assigned to the parameter type 'List<dynamic Function(Store<AppState/_2/>, dynamic, dynamic Function(dynamic))>'. - 'List' is from 'dart:core'. - 'Store' is from 'package:redux/src/store.dart' ('/C:/flutter/.pub-cache/hosted/pub.dartlang.org/redux-4.0.0+3/lib/src/store.dart'). - 'AppState/_1/' is from 'package:maghsala_customer/states/AppState.dart' ('lib/states/AppState.dart'). - 'AppState/_2/' is from '/D://maghsala//maghsala_customer//lib//states/AppState.dart'. createStoreMiddleWare(LoginRepository(), UserRepository()), ^ /D://maghsala//maghsala_customer//lib//main.dart:61:60: Error: A value of type 'Stream<ConnectivityStatus/_1/>' can't be assigned to a variable of type 'Stream<ConnectivityStatus/_2/>'. - 'Stream' is from 'dart:async'. - 'ConnectivityStatus/_1/' is from 'package:maghsala_customer/enums/connectivity_status.dart' ('lib/enums/connectivity_status.dart'). - 'ConnectivityStatus/_2/' is from '/D://maghsala//maghsala_customer//lib//enums/connectivity_status.dart'. ConnectivityService().connectionStatusController.stream, ^ FAILURE: Build failed with an exception. - Where: Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 904 - What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1 - Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. - Get more help at https://help.gradle.org BUILD FAILED in 34s Exception: Gradle task assembleDebug failed with exit code 1
解决方案
统一导入路径
检查所有文件中导入AppState、ConnectivityStatus等本地类的语句,确保全部使用package:格式导入,禁止混用绝对路径或相对路径。例如:- 替换绝对路径导入:
import '/D://maghsala//maghsala_customer//lib//states/AppState.dart'; - 改为package路径:
import 'package:maghsala_customer/states/AppState.dart';
- 替换绝对路径导入:
清理项目缓存
依次执行以下命令:flutter clean flutter pub get之后重启你的IDE(如VS Code/Android Studio),必要时清除IDE缓存(Android Studio可通过
File -> Invalidate Caches / Restart操作)。验证pubspec配置
打开项目根目录的pubspec.yaml文件,确认name字段值为maghsala_customer——这是package路径导入的核心依据,若名称不匹配会导致导入识别异常。检查类型定义一致性
确认appReducer的参数、返回类型,以及中间件createStoreMiddleWare的泛型约束,均使用统一导入后的AppState类型,避免因导入错误残留的类型不一致问题。
内容的提问来源于stack exchange,提问作者Dark Legend
相关产品推荐
相关产品推荐

