如何将文本发送到其他页面? 'package:flutter/src/material/tab_controller.dart':断言失败:第 181 行 pos 12:'值 >= 0

How to send text to other page? 'package:flutter/src/material/tab_controller.dart': Failed assertion: line 181 pos 12: 'value >= 0

帮帮我,我想将文本从第一页发送到第二页。

首页

Padding(
              padding: const EdgeInsets.only(top: 16.0),
              child: RaisedGradientButton(
                  child: Text(
                    'Dapatkan Pengukuran',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0,
                        fontFamily: 'Nunito',
                        fontWeight: FontWeight.bold),
                  ),
                  gradient: LinearGradient(
                    colors: const <Color>[Colors.purple, Colors.blue],
                  ),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ShowMeasurementsPage(                            
                              name: name.text,
                              nohap: nohap.text,
                            
                      ),
                 ),
         );
   }),
)

第二页*

我想在第二页显示文本,但出现错误。

import 'package:flutter/material.dart';

class ShowMeasurementsPage extends StatefulWidget {
  static String tag = 'show-measurements-page';
  const ShowMeasurementsPage({Key? key}) : super(key: key);
  @override
  _ShowMeasurementsPageState createState() => _ShowMeasurementsPageState();
}

class _ShowMeasurementsPageState extends State<ShowMeasurementsPage> {
  final String? name, nohap;
  _ShowMeasurementsPageState({this.name, this.nohap});

谁能帮帮我?

您需要将参数传递给 StatefulWidget,然后您可以使用 widget.namewidget.nohap.

build 方法中访问它们

例如:

class ShowMeasurementsPage extends StatefulWidget {
  static String tag = 'show-measurements-page';
  final String? name;
  final String? nohap;

  const ShowMeasurementsPage({
    Key? key,
    this.name,
    this.nohap,
  }): super(key: key);

  @override
  State<ShowMeasurementsPage> createState() => 
    _ShowMeasurementsPageState();
}

class _ShowMeasurementsPageState extends State<ShowMeasurementsPage> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if (widget.name != null)
          Text(widget.name!),
        if (widget.nohap != null)
          Text(widget.nohap!),
      ],
    );
  }
}