Error: Too many positional arguments: 1 allowed, but 2 found

1k views Asked by At

I'm working on a flutter app that is getting a JSON from an API. Then I'm parsing the JSON and building a List Tile. When I try to run the code, I'm getting this error:

lib/screens/screen4.dart:112:23: Error: Too many positional arguments: 1 allowed, but 2 found.
Try removing the extra positional arguments.
          return _tile(data[index].ssid,data[index].auth,icon: Icons.wifi);     

This is my code:

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(
    JobsListView(),
  );
}

class Job {
  final String ssid;
  final String auth;
  final String encry;

  Job({required this.ssid, required this.auth, required this.encry});

  factory Job.fromJson(Map<String, dynamic> json) {
    return Job(
      ssid: json['ssid'],
      auth: json['auth'],
      encry: json['encry'],
    );
  }
}

class JobsListView extends StatelessWidget {
  const JobsListView({Key? key}) : super(key: key);

  Future<List<Job>> _fetchJobs() async {
    final response = await http.get(
        Uri.parse('http://10.10.10.254/httpapi.asp?command=wlanGetApListEx'));

    if (response.statusCode == 200) {
      final List jsonResponse = json.decode(response.body)['aplist'] as List;
      return jsonResponse.map((job) => new Job.fromJson(job)).toList();
    } else {
      throw Exception('Failed to load jobs from API');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Finding your available networks',
            style: TextStyle(color: Colors.black87)),
        titleSpacing: 00.0,
        centerTitle: true,
        toolbarHeight: 60.2,
        toolbarOpacity: 0.6,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              bottomRight: Radius.circular(25),
              bottomLeft: Radius.circular(25)),
        ),
        elevation: 0.00,
        backgroundColor: Colors.transparent,
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              child: FutureBuilder<List<Job>>(
                future: _fetchJobs(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    List<Job> data = snapshot.data ?? [];
                    return _jobsListView(data);
                  } else if (snapshot.hasError) {
                    return Text("${snapshot.error}");
                  }
                  return Container(
                    alignment: Alignment.topCenter,
                    margin: EdgeInsets.only(top: 400),
                    child: CircularProgressIndicator(
                      backgroundColor: Colors.grey,
                      color: Colors.black,
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  ListView _jobsListView(data) {
    return ListView.builder(
        itemCount: data.length,
        itemBuilder: (context, index) {
          return _tile(data[index].ssid, data[index].auth, icon: Icons.wifi);
        });
  }

  ListTile _tile(BuildContext context,
          {required String title,
          required String subtitle,
          required IconData icon}) =>
      ListTile(
        title: Text(title,
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontSize: 20,
            )),
        subtitle: Text(subtitle),
        leading: Icon(
          icon,
          color: Colors.grey[500],
        ),
        trailing: Icon(
          Icons.arrow_forward_ios,
        ),
        onTap: () {
          Navigator.pushNamed(context, '/fifth');
        },

        //Navigator.pushNamed(context, '/fifth'),

        // => print('on tap'),
        //TO DO: Pass the arguments selected to the next screen, and insert it into the URI
        //TO DO:Hex to ASCII.
      );
}

My intention is to construct the ListTile and then navigate from each Tile to another screen. Why am I getting this error?

3

There are 3 answers

0
Md. Yeasin Sheikh On BEST ANSWER

You are using one positional parameter, and 4 required name parameter.

tTile _tile(BuildContext context,
          {required String title,
          required String subtitle,
          required IconData icon}) =>

You need to pass data like

return _tile(
        context,
        subtitle:data[index].ssid,
        title: data[index].auth,
        icon: Icons.wifi,
      );

More about using using-constructors

1
Kerby Elpenord On
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;



void main() {
  runApp(
    const JobsListView(),
  );
}

class Job {
  final String ssid;
  final String auth;
  final String encry;

  Job({required this.ssid, required this.auth,required this.encry});

  factory Job.fromJson(Map<String, dynamic> json) {
    return Job(
      ssid: json['ssid'],
      auth: json['auth'],
      encry: json['encry'],
    );
  }
}

class JobsListView extends StatelessWidget {
  const JobsListView({Key? key}) : super(key: key);


  Future<List<Job>> _fetchJobs() async {
    final response = await http
        .get(
        Uri.parse('http://10.10.10.254/httpapi.asp?command=wlanGetApListEx'));

    if (response.statusCode == 200) {
      final List jsonResponse = json.decode(response.body)['aplist'] as List;
      return jsonResponse.map((job) =>  Job.fromJson(job)).toList();
    } else {
      throw Exception('Failed to load jobs from API');
    }
  }





  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: 
      Scaffold(
        appBar: AppBar(
          title: const Text('Finding your available networks',
              style: TextStyle(color: Colors.black87)),

          titleSpacing: 00.0,
          centerTitle: true,
          toolbarHeight: 60.2,
          toolbarOpacity: 0.6,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                bottomRight: Radius.circular(25),
                bottomLeft: Radius.circular(25)),
          ),
          elevation: 0.00,
          backgroundColor: Colors.transparent,


        ),
        body: SafeArea(
          child: Column(
            children: <Widget>[
              Expanded(
                child: FutureBuilder<List<Job>>(
                  future: _fetchJobs(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      List<Job> data = snapshot.data ?? [];
                      return _jobsListView(data);
                    } else if (snapshot.hasError) {
                      return Text("${snapshot.error}");
                    }
                    return Container(
                      alignment: Alignment.topCenter,
                      margin: const EdgeInsets.only(top: 400),
                      child: const CircularProgressIndicator(
                        backgroundColor: Colors.grey,
                        color: Colors.black,
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }



  ListView _jobsListView(data) {
    return ListView.builder(
        itemCount: data.length,
        itemBuilder: (context, index) {
          return _tile(context,  data[index].ssid, data[index].auth ,Icons.wifi);
        });
  }


  ListTile _tile(BuildContext context,  String title, String subtitle, IconData icon) =>
      ListTile(
        title: Text(title,
            style: const TextStyle(
              fontWeight: FontWeight.w500,
              fontSize: 20,
            )),
        subtitle: Text(subtitle),
        leading: Icon(
          icon,
          color: Colors.grey[500],
        ),
        trailing: const Icon(
          Icons.arrow_forward_ios,
        ),
        onTap: ()


        {
          Navigator.pushNamed(context, '/fifth');
        },







        //Navigator.pushNamed(context, '/fifth'),

        // => print('on tap'),
        //TO DO: Pass the arguments selected to the next screen, and insert it into the URI
        //TO DO:Hex to ASCII.
      );
}

It should work right now

0
TarHalda On

You are passing too many positional parameters. If an argument is declared in curly braces, like title and subtitle are, then you have to use its name when calling the method. You are also not passing the BuildContext, which is the only argument not declared in curly braces.

Change it to:

return _tile(
    context, 
    title: data[index].ssid,
    subtitle: data[index].auth,
    icon: Icons.wifi
);