无法在flutter的两页之间传递null变量

时间:2018-11-09 15:39:52

标签: dart flutter

我正在尝试在两个页面之间导航。在第二页中,我需要一个构造函数,因此需要在第一页中将变量声明为null,然后在第二页中对其进行初始化。

main.dart (第一页)中,我类似于

PostDetailsPage.tag: (context) => PostDetailsPage(new List()),

在第二页( PostDetailsPage )中,我有这个

final List<charts.Series> seriesList;
final bool animate;

PostDetailsPage(this.seriesList, {this.animate});

当我转到第二页时返回

  

<列表<系列<动态,动态>>不是类型<列表<系列<动态,字符串>>

的子类型

那我该如何解决?初始化第一页中的值并通过它吗?在第一页中将值初始化为null并通过它吗?

已更新

Main.dart类

import 'package:flutter/material.dart';
import 'package:fluttercrud/screens/login_page.dart';
import 'package:fluttercrud/screens/home_page.dart';
import 'package:fluttercrud/screens/partials/list_post.dart';
import 'package:fluttercrud/screens/maps_page.dart';
import 'package:fluttercrud/screens/post_details_page.dart';


void main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  final routes = <String, WidgetBuilder>{
    LoginPage.tag: (context) => LoginPage(),
    HomePage.tag: (context) => HomePage(),
    ListPost.tag: (context) => ListPost(),
    MapsPage.tag: (context) => MapsPage(),
    PostDetailsPage.tag: (context) => PostDetailsPage(new List()),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'iGota',    
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch  : Colors.blue,
      ),
      home: LoginPage(),
      routes: routes,
    );
  }
}

PostDetailsPage.dart类

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart'  as charts;


class PostDetailsPage extends StatelessWidget{
    static String tag = 'post-details-page';
  final List<charts.Series> seriesList;
  final bool animate;

  PostDetailsPage(this.seriesList, {this.animate});

  /// Creates a [BarChart] with sample data and no transition.
  factory PostDetailsPage.withSampleData() {
    return new PostDetailsPage(
      _createSampleData(),
      // Disable animations for image tests.
      animate: false,
    );
  }


  @override
  Widget build(BuildContext context) {
    return new charts.BarChart(
      seriesList,
      animate: animate,
    );
  }

  /// Create one series with sample hard coded data.
  static List<charts.Series<OrdinalSales, String>> _createSampleData() {
    final data = [
      new OrdinalSales('2014', 5),
      new OrdinalSales('2015', 25),
      new OrdinalSales('2016', 100),
      new OrdinalSales('2017', 75),
    ];

    return [
      new charts.Series<OrdinalSales, String>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

/// Sample ordinal data type.
class OrdinalSales {
  final String year;
  final int sales;

  OrdinalSales(this.year, this.sales);
}

更新2:已修复

Main.dart

import 'package:flutter/material.dart';
import 'package:fluttercrud/screens/login_page.dart';
import 'package:fluttercrud/screens/home_page.dart';
import 'package:fluttercrud/screens/partials/list_post.dart';
import 'package:fluttercrud/screens/maps_page.dart';
import 'package:fluttercrud/screens/post_details_page.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final routes = <String, WidgetBuilder>{
    LoginPage.tag: (context) => LoginPage(),
    HomePage.tag: (context) => HomePage(),
    ListPost.tag: (context) => ListPost(),
    MapsPage.tag: (context) => MapsPage(),
    PostDetailsPage.tag: (context) => PostDetailsPage(),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'iGota',    
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch  : Colors.blue,
      ),
      home: LoginPage(),
      routes: routes,
    );
  }
}

PostDetailsPage.dart

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;

class PostDetailsPage extends StatefulWidget {
  static String tag = 'post-details-page';

  @override
  PostDetailsPageState createState() => new PostDetailsPageState();
}

class PostDetailsPageState extends State<PostDetailsPage> {
  List<charts.Series> seriesList = [];
  bool animate;

  void initState() {
    super.initState();
    seriesList = _createSampleData();
    animate = false;
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      decoration: new BoxDecoration(color: Colors.white),
      child: new charts.BarChart(
        seriesList,
        animate: animate,
      ),
    );
  }

  /// Create one series with sample hard coded data.
  static List<charts.Series<OrdinalSales, String>> _createSampleData() {
    final data = [
      new OrdinalSales('2014', 5),
      new OrdinalSales('2015', 25),
      new OrdinalSales('2016', 100),
      new OrdinalSales('2017', 75),
    ];

    return [
      new charts.Series<OrdinalSales, String>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

/// Sample ordinal data type.
class OrdinalSales {
  final String year;
  final int sales;

  OrdinalSales(this.year, this.sales);
}

1 个答案:

答案 0 :(得分:0)

您可以通过在传入List时明确声明其类型来解决问题。

PostDetailsPage.tag: (context) => PostDetailsPage(List<charts.Series<dynamic, String>>()),

或者,您可以在页面本身上显式声明列表本身的类型:

final List<charts.Series<dynamic, String>> seriesList;

我认为图表库工作的预期方式是传递一个已经初始化的图表数据列表,然后页面将呈现这些列表。如果只想返回给定的样本数据,则应使用给定的工厂方法PostDetailsPage.withSampleData()