fluttercommunity / redux.dart

Redux for Dart
https://pub.dev/packages/redux
MIT License
515 stars 61 forks source link

The argument type '(#lib1::AppState, dynamic) → #lib1::AppState' can't be assigned to the parameter type '(#lib2::AppState, dynamic) → #lib2::AppState'. #42

Closed rob-nolan closed 5 years ago

rob-nolan commented 5 years ago

I get the above compiler error when building my flutter app, the error relates to the appReducer parameter in the Store instantiation in main.dart. I've searched and found people having similar issues, but in their case it was always caused by relative paths which I am not using.

I've gone through my code and the docs a number of times and I can't see anything wrong so I think it may be a bug but please correct me if I am missing something.

This may be an issue with flutter_redux rather than redux.dart so I've opened an issue there also.

-main.dart-
class MainApp extends StatelessWidget {
  final store = Store<AppState>(
    appReducer, 
    initialState: new AppState(),
    middleware: []
      ..addAll(createAuthMiddleware())
      ..add(new LoggingMiddleware.printer()),
  );
.
.
.
-app_state.dart-
class AppState {
  final User currentUser;

  AppState({
    this.currentUser,
  });

  AppState copyWith({User currentUser}) {
    return new AppState(
      currentUser: currentUser ?? this.currentUser,
    );
  }

  @override
  String toString() {
    return 'AppState{currentUser: $currentUser}';
  }
}
-auth_actions.dart-
class LogIn {}

class LogInSuccessful {
  final User user;

  LogInSuccessful({ @required this.user});

  @override
  String toString() {
    return 'LogIn{user: $user}';
  }
}

class LogInFail {
  final dynamic error;
  LogInFail(this.error);
  @override
  String toString() {
    return 'LogIn{There was an error logging in: $error}';
  }
}

class LogOut {}

class LogOutSuccessful {
  LogOutSuccessful();
  @override
  String toString() {
    return 'LogOut{user: null}';
  }
}
-app_reducer.dart-
AppState appReducer(AppState state, dynamic action) {
  return new AppState(
    currentUser: authReducer(state.currentUser, action)
  );
}
-auth_reducer.dart-
final authReducer = combineReducers<User>([
  TypedReducer<User, LogInSuccessful>(_logIn),
  TypedReducer<User, LogOut>(_logOut),
]);

User _logIn(User user, dynamic action) {
  return action.user;
}

Null _logOut(User user, dynamic action) {
  return null;
}
-pubspec.yaml-
environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  redux: ^3.0.0
  flutter_redux: ^0.5.2
flutter --version
Flutter 0.8.2 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 5ab9e70727 (3 weeks ago) • 2018-09-07 12:33:05 -0700
Engine • revision 58a1894a1c
Tools • Dart 2.1.0-dev.3.1.flutter-760a9690c2
brianegan commented 5 years ago

Thanks! Yep, this is better handled on the flutter_redux side! It almost always has to do with conflicting imports, which have changed a bit and later version of Flutter seem to handle this much better. Sorry for missing this one -- if you're still having trouble please write back :)