ericwindmill/flutter_by_example_apps

Redux appReducer dynamic type

Fouray opened this issue · 4 comments

Followed the redux example and when run for the basic counter go t this error
compiler message: lib/main.dart:21:7: Error: A value of type '(dynamic, dynamic) → #lib1::AppState' can't be assigned to a variable of type '(#lib2::AppState, dynamic) → #lib2::AppState'. compiler message: Try changing the type of the left hand side, or casting the right hand side to '(#lib2::AppState, dynamic) → #lib2::AppState'. compiler message: appReducer, compiler message: ^ Compiler failed on L:\Projects\Flutter\base_app\base_app\lib\main.dart

main.dart
`
void main() => runApp(new MainApp());

class MainApp extends StatelessWidget {
final String title = 'MeSuite';

MainApp();

@OverRide
Widget build(BuildContext context) {
var store;
store = new Store(
appReducer,
initialState: new AppState(),
distinct: true,
middleware: []
);

return new StoreProvider(
  store: store,
  child: new MaterialApp(
    debugShowCheckedModeBanner: false,
    title: title,
    home:Welcome(title),
  ),
);

}
}`

app_reducer.dart AppState appReducer(state, action) { return new AppState( isLoading: false, count: counterReducer(state.count, action), ); //new }

`class AppState {
final int count;
final bool isLoading;

AppState({
this.count = 0,
this.isLoading = false,
});
model.dart
factory AppState.loading() => new AppState(isLoading: true);

AppState copyWith({int count, bool isLoading}) {
return new AppState(
count: count ?? this.count,
isLoading: isLoading ?? this.isLoading,
);
}

@OverRide

bool operator ==(Object other) =>
identical(this, other) ||
other is AppState &&
runtimeType == other.runtimeType &&
isLoading == other.isLoading &&
count == other.count ;

@OverRide
String toString() {
return 'AppState{isLoading: $isLoading, count: $count}';
}
}`

This may have been just a directory strucure thing.

When i moved the app_state.adart file out of the models directory to the lib directory the error went away.

I had the same error message because I was importing a file using different case sensitivity than the actual filename was (on macOS).

If you see this error make sure you double check the import path!

It actually has nothing to do with folder structure it's
import 'package:projname/models/app_state.dart';
vs
import 'models/app_state.dart';

This way if you mix them, there are different imports lib1 and lib2

So I'm guessing this issue can be closed? Feel free to tell me if that's not true.