Flutter尝试更改参数的类型或强制转换参数

时间:2019-05-15 14:47:51

标签: dart flutter

我不明白这个错误,有时会突然出现,而其他时候却保持沉默。

从典型的React&Redux类型移植应用程序,我正在尝试实现Redux(它已经实现并可以正常工作)。我处理主题的模型之一如下:

import 'package:flutter_web/material.dart';

enum ThemeType { light, dark }

var defaultTheme = ThemeModel(
    currentTheme: ThemeType.light,
    colors: {
        "primary": Colors.blue,
        "accent": Colors.pinkAccent[100],
        "primaryColorLight": Colors.white
    },
    textTheme: {
        "headline": TextStyle(
            fontSize: 72.0, fontWeight: FontWeight.bold
        ),
        "title": TextStyle(
            fontSize: 36.0, color: Colors.blueGrey
        ),
        "body1": TextStyle(fontSize: 14.0)
    },
    brightness: Brightness.light,
    fontFamily: "Roboto"
);

class ThemeModel {
    ThemeType currentTheme;
    String fontFamily;
    Brightness brightness;
    Map<String, Color> colors;
    Map<String, TextStyle> textTheme;

    ThemeModel({
        this.currentTheme,
        this.fontFamily,
        this.brightness,
        this.colors,
        this.textTheme
    });

    factory ThemeModel.initialState() => defaultTheme;

    ThemeModel setDark() {
        //  Must return a new instance
        return new ThemeModel(
            currentTheme: ThemeType.dark,
            colors: new Map<String, Color>.from(defaultTheme.colors)
                ..addAll({
                    "primary": Colors.amber,
                }),
            textTheme: defaultTheme.textTheme,
            brightness: Brightness.dark,
            fontFamily: defaultTheme.fontFamily,
        );
    }

    ThemeModel setLight() {
        return defaultTheme;
    }
}

应用程序的主要部分(入口点)使用一个视图模型来提取此主题信息:

class Typely extends StatelessWidget {
    final store = new Store<AppState>(
        appStateReducer,
        initialState: new AppState.initialState(),
        middleware: createAppMiddleware()
    );

    @override
    Widget build(BuildContext context) {
        return StoreProvider(
            store: store,
            child: StoreConnector<AppState, _ViewModel>(
                converter: _ViewModel.fromStore,
                builder: (context, vm) {
                    return MaterialApp(
                        title: 'Free online proofreading and essay editor - Typely',
                        debugShowCheckedModeBanner: false,
                        theme: ThemeData(
                            brightness: vm.theme.brightness,
                            primaryColor: vm.theme.colors["primary"],
                            accentColor: vm.theme.colors["accent"],
                            fontFamily: vm.theme.fontFamily,
                            textTheme: TextTheme(
                                headline: vm.theme.textTheme["headline"],
                                title: vm.theme.textTheme["title"],
                                body1: vm.theme.textTheme["body1"],
                            ),
                        ),
                        routes: {
                            "/": (context) => PageWrapper(child: HomePage()),
                        },
                    );
                }
            ),
        );
    }
}

class _ViewModel {
    final ThemeModel theme;

    _ViewModel({
        @required this.theme,
    });

    static _ViewModel fromStore(Store<AppState> store) {
        return _ViewModel(
            theme: store.state.theme,
        );
    }
}

应用程序有30%的时间运行,但是在我进行更改时,经常会出现此错误:

Error compiling dartdevc module:typely|lib/main.ddc.js

packages/typely/main.dart:65:23: Error: The argument type 'ThemeModel/*1*/' can't be assigned to the parameter type 'ThemeModel/*2*/'.
 - 'ThemeModel/*1*/' is from 'package:typely/store/models/_theme.dart' ('packages/typely/store/models/_theme.dart').
 - 'ThemeModel/*2*/' is from 'package:typely/store/models/_theme.dart' ('packages/typely/store/models/_theme.dart').
Try changing the type of the parameter, or casting the argument to 'ThemeModel/*2*/'.
            theme: store.state.theme,
                               ^

从我正在阅读的内容来看,这可能与以下事实有关:该模型的导入并不总是相同,但我进行了三遍检查,并使用来自软件包的绝对导入是一致的:

import 'package:typely/store/models/_theme.dart';

0 个答案:

没有答案