dart Flutter微件不是子类型

eqqqjvef  于 2023-09-28  发布在  Flutter
关注(0)|答案(1)|浏览(99)

我正在为我的Flutter文件创建一个基本状态。我创建了一个基本结构,但我不能在我的页面上使用它,我得到这个错误。

错误

type '(BuildContext,SplashViewModel)=> Widget' is not a subtype of type '(BuildContext,dynamic)=> Widget'参见:https://flutter.dev/docs/testing/错误

SplashView

import 'package:flutter/material.dart';
import 'package:motiontraq/core/base/state/base_state.dart';
import 'package:motiontraq/core/base/view/base_widget.dart';
import 'package:motiontraq/view/authentication/splash/splash_view_model.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends BaseState<SplashScreen> {
  SplashViewModel viewModel = SplashViewModel();

  // onModelReady işlevini dynamic türünde tanımla
  void onModelReady(dynamic model) {
    viewModel =
        model as SplashViewModel; // Modeli SplashViewModel olarak dönüştür
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<SplashViewModel>(
      viewModel: viewModel,
      onModelReady: onModelReady, // onModelReady işlevini atayın
      onPageBuilder: (context, value) => scaffoldBody(),
    );
  }

  Widget scaffoldBody() {
    return Scaffold(
      body: Center(child: Text(viewModel.deneme)),
    );
  }
}

BaseWidget

import 'package:flutter/material.dart';

// Sonradan Gerekebilir diye BaseView oluşturuldu ve içi boş bırakıldı.

class BaseView<T> extends StatefulWidget {
  const BaseView({
    required this.viewModel,
    required this.onPageBuilder,
    super.key,
    this.onModelReady,
    this.onDispose,
  });
  final Widget Function(BuildContext context, T value) onPageBuilder;
  final T viewModel;
  final void Function(T model)? onModelReady;
  final VoidCallback? onDispose;

  @override
  State<BaseView> createState() => _BaseViewState();
}

class _BaseViewState extends State<BaseView> {
  @override
  void initState() {
    super.initState();
    widget.onModelReady?.call(widget.viewModel);
  }

  @override
  void dispose() {
    super.dispose();
    widget.onDispose?.call();
  }

  @override
  Widget build(BuildContext context) {
    return widget.onPageBuilder(context, widget.viewModel);
  }
}

SplashViewModel

class SplashViewModel {
  String deneme = "deneme";
}

BaseState

import 'package:flutter/material.dart';

abstract class BaseState<T extends StatefulWidget> extends State<T> {
  ThemeData get currentTheme => Theme.of(context);

  double dynamicHeight(double value) =>
      MediaQuery.of(context).size.height * value;
  double dynamicWidth(double value) =>
      MediaQuery.of(context).size.width * value;
  double screenWidth() => MediaQuery.of(context).size.width;
  double screenHeight() => MediaQuery.of(context).size.height;
}
zpf6vheq

zpf6vheq1#

我不是很确定为什么要这样但是改变这条线

onPageBuilder: (context, value) => scaffoldBody(),

onPageBuilder: (context, dynamic value) => scaffoldBody(),

似乎解决了这个问题,但不可否认的是,如果您实际上想使用值作为指示的类型,那么这远非理想。我看到对于onModelReady,您实际上需要执行相同的操作

相关问题