本文介绍了参数不匹配的闭包调用:function' _myListView.functionOne'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么此错误会出现在控制台框中.一切看起来都很好,但下面仍然出现

I don't know why this error is appearing in the console box. everything appears fine but still this below appears

这是我用于列表查看的代码


import 'package:flutter/material.dart';

import 'package:linear_gradient/linear_gradient.dart';
import 'package:gradient_text/gradient_text.dart';
import 'package:apple_beauty/cdetails.dart';



Gradient gradient = LinearGradient(
    colors: [Colors.teal, Colors.deepOrange, Colors.pink]);


class RamList extends StatefulWidget {
  @override
  _RamListState createState() => _RamListState();
}

class _RamListState extends State<RamList> {
  @override
  Widget build(BuildContext context) {
    return _myListView(context);
  }
}




Widget _myListView(BuildContext context) {
  final titles = [
    '1st',
    '2nd',
    '3rd',
    '4th',
    '5th',
                ];

  final numbers = [
    '1 ',
    '2 ',
    '3 ',
    '4 ',
    '5 ',



  ];

  functionOne() {
    Navigator.push(
        context, new MaterialPageRoute(builder: (context) =>  RamOne()));
  }

  functionTwo() {
    Navigator.push(context, MaterialPageRoute(builder: (context) => RamTwo()));
  }

  functionThree() {
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => RamThree()));
  }

  functionFour() {
    Navigator.push(context, MaterialPageRoute(builder: (context) => RamFour()));
  }




  functionFive(BuildContext context)=>_onAlertButtonPressed1(context);





  final List<Function> onTaps = [
    functionOne,
    functionTwo,
    functionThree,
    functionFour,
    functionFive,

  ];

  return ListView.builder(

      itemCount: titles.length,
      itemBuilder: (context, index) {
        return Card(
            elevation: 50,
            child: InkWell(


              splashColor: Colors.green,
              highlightColor: Colors.red,
              child: Row(
                children: <Widget>[


                  Container(


                    height: 100.0,
                    width:50.0,

                    decoration: BoxDecoration(
                        gradient:LinearGradientStyle.linearGradient(
                            orientation:LinearGradientStyle.ORIENTATION_HORIZONTAL,
                            gradientType: LinearGradientStyle.GRADIENT_TYPE_AMIN
                        )
                    ),),

                  Container(
                      margin: EdgeInsets.all(10),
                      child: Text(
                        numbers[index],
                      )),


                  Flexible(child:  Container(
                    margin: EdgeInsets.all(10),
                    child: GradientText((titles[index]),
                      gradient:gradient,

                      style:TextStyle(fontSize:20.0,fontWeight:FontWeight.bold, ),
                    ),
                    //Text(titles[index]),
                  ))
                ],
              ),
              onTap: () => onTaps[index](context),
            ));
      });


}

_onAlertButtonPressed1(context) {
  AlertDialog alert = AlertDialog(
    title: Text('Coming soon'),
    content: Text("This link will be available in future updates"),
    actions: [

    ],
  );


  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}


这是我用于LISTVIEW导航详细信息的代码

import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
//import 'package:html/dom.dart' as dom;
import 'package:gradient_app_bar/gradient_app_bar.dart';

class RamOne extends StatefulWidget {
  RamOne({Key key, this.title}) : super(key: key);

  final String title;

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

class _RamOneState extends State<RamOne> {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar:   GradientAppBar(
            gradient: LinearGradient(
                colors: [Colors.green, Colors.greenAccent]),
          title:  Text('Overview'),
        ),
        body: new Center(
          child: SingleChildScrollView(
            child: Html(

              data: """ <h1>1st page</h1>


  """,

            ),
          ),
        ));
  }}


class RamTwo extends StatefulWidget {
  RamTwo({Key key, this.title}) : super(key: key);

  final String title;

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

class _RamTwoState extends State<RamTwo> {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar:   GradientAppBar(
          gradient: LinearGradient(
              colors: [Colors.green, Colors.greenAccent]),
          title:  Text('Nutritional Value'),
        ),
        body: new Center(
          child: SingleChildScrollView(
            child: Html(
              data: """
<h1>1st page</h1>""",

            ),
          ),
        ));
  }}


class RamThree extends StatefulWidget {
  RamThree({Key key, this.title}) : super(key: key);

  final String title;

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

class _RamThreeState extends State<RamThree> {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar:   GradientAppBar(
          gradient: LinearGradient(
              colors: [Colors.green, Colors.greenAccent]),
          title:  Text('Types of apple'),
        ),
        body: new Center(
          child: SingleChildScrollView(
            child: Html(
              data: """
              <h1>1st page</h1>
  """,

            ),
          ),
        ));
  }}


class RamFour extends StatefulWidget {
  RamFour({Key key, this.title}) : super(key: key);

  final String title;

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

class _RamFourState extends State<RamFour> {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar:   GradientAppBar(
          gradient: LinearGradient(
              colors: [Colors.green, Colors.greenAccent]),
          title:  Text('Good for Neuron'),
        ),
        body: new Center(
          child: SingleChildScrollView(
            child: Html(
              data: """
     <h1>1st page</h1>
  """,

            ),
          ),
        ));
  }}





  }}


推荐答案

您忘记将参数 BuildContext上下文添加到函数中
代码段

You forgot to add parameter BuildContext context to your function
code snippet

functionOne(BuildContext context) {
    Navigator.push(
        context, new MaterialPageRoute(builder: (context) => RamOne()));
  }

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:linear_gradient/linear_gradient.dart';
import 'package:gradient_text/gradient_text.dart';
//import 'package:apple_beauty/cdetails.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(flex:5, child: RamList()),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Gradient gradient =
    LinearGradient(colors: [Colors.teal, Colors.deepOrange, Colors.pink]);

class RamList extends StatefulWidget {
  @override
  _RamListState createState() => _RamListState();
}

class _RamListState extends State<RamList> {
  @override
  Widget build(BuildContext context) {
    return _myListView(context);
  }
}

Widget _myListView(BuildContext context) {
  final titles = [
    '1st',
    '2nd',
    '3rd',
    '4th',
    '5th',
  ];

  final numbers = [
    '1 ',
    '2 ',
    '3 ',
    '4 ',
    '5 ',
  ];

  functionOne(BuildContext context) {
    Navigator.push(
        context, new MaterialPageRoute(builder: (context) => RamOne()));
  }

  functionTwo(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => RamTwo()));
  }

  functionThree(BuildContext context) {
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => RamThree()));
  }

  functionFour(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => RamFour()));
  }

  functionFive(BuildContext context) => _onAlertButtonPressed1(context);

  final List<Function> onTaps = [
    functionOne,
    functionTwo,
    functionThree,
    functionFour,
    functionFive,
  ];

  return ListView.builder(
      itemCount: titles.length,
      itemBuilder: (context, index) {
        return Card(
            elevation: 50,
            child: InkWell(
              splashColor: Colors.green,
              highlightColor: Colors.red,
              child: Row(
                children: <Widget>[
                  Container(
                    height: 100.0,
                    width: 50.0,
                    decoration: BoxDecoration(
                        gradient: LinearGradientStyle.linearGradient(
                            orientation:
                                LinearGradientStyle.ORIENTATION_HORIZONTAL,
                            gradientType:
                                LinearGradientStyle.GRADIENT_TYPE_AMIN)),
                  ),
                  Container(
                      margin: EdgeInsets.all(10),
                      child: Text(
                        numbers[index],
                      )),
                  Flexible(
                      child: Container(
                    margin: EdgeInsets.all(10),
                    child: GradientText(
                      (titles[index]),
                      gradient: gradient,
                      style: TextStyle(
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    //Text(titles[index]),
                  ))
                ],
              ),
              onTap: () => onTaps[index](context),
            ));
      });
}

_onAlertButtonPressed1(context) {
  AlertDialog alert = AlertDialog(
    title: Text('Coming soon'),
    content: Text("This link will be available in future updates"),
    actions: [],
  );

  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

class RamOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("RamOne");
  }
}

class RamTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("RamTwo");
  }
}

class RamThree extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("RamThree");
  }
}

class RamFour extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("RamFour");
  }
}

这篇关于参数不匹配的闭包调用:function&amp;#39; _myListView.functionOne&amp;#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:28