Hello guys im new to flutter and was trying to make a news app with API. but i have an error in my homepage class, in
itemBuilder: (context, index) => listTile(articles[index])
in the (articles[index])
there is a red underland says "2 positional argument(s) expected, but 1 found. Try adding the missing". this error happen after i add "BuildContext context" to add an InkWell widget in my ListTile class
Widget listTile(Article article, BuildContext context) {
return InkWell(
onTap: (){
Navigator.push(
context, MaterialPageRoute(
builder: (
context) => NewsDetail(
article: article))
);
},
so here is my full homepage class code
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:medreminder/NewsArticle/components/list_tile.dart';
import 'package:medreminder/NewsArticle/models/article_models.dart';
import 'package:medreminder/NewsArticle/services/api_service.dart';
class NewsHomePage extends StatelessWidget {
//const NewsHomePage({super.key});
ApiService client = ApiService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Get.isDarkMode?Colors.grey[600]:Colors.white,
leading: IconButton(
onPressed: ()=>Get.back(),
icon: Icon(Icons.arrow_back_ios,
color: Get.isDarkMode?Colors.white:Colors.grey
),
),
title: Text("News & Article", style: TextStyle(
color: Get.isDarkMode?Colors.white:Colors.black
),),
),
body: FutureBuilder<List<Article>>(
future: client.getArticle(),
builder: (BuildContext context, AsyncSnapshot<List<Article>> snapshot) {
if(snapshot.hasData&&snapshot.data!=null&& snapshot.data!.isNotEmpty){
List<Article>? articles = snapshot.data;
return ListView.builder(
itemCount: articles!.length,
itemBuilder: (context, index) => listTile(articles[index])
);
}
return Center(child: CircularProgressIndicator(),);
},
),
);
}
}
and here is my ListTile class
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:medreminder/NewsArticle/models/article_models.dart';
import 'package:medreminder/NewsArticle/pages/news_detail.dart';
import 'package:medreminder/Reminder/ui/theme.dart';
Widget listTile(Article article, BuildContext context) {
return InkWell(
onTap: (){
Navigator.push(
context, MaterialPageRoute(
builder: (
context) => NewsDetail(
article: article))
);
},
child: Container(
margin: EdgeInsets.all(12),
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(13),
boxShadow: [BoxShadow(color: Colors.black, blurRadius: 3)]),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 180,
width: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(image: NetworkImage(article.urlToImage!),
fit: BoxFit.cover),
borderRadius: BorderRadius.circular(10),
),
),
SizedBox(height: 9),
Container(
padding: EdgeInsets.all(6),
decoration: BoxDecoration(
color: lightblueColor,
borderRadius: BorderRadius.circular(20),
),
child: Text(article.source!.name!, style: TextStyle(color: Get.isDarkMode?Colors.white:Colors.black),),
),
SizedBox(
height: 8,
),
Text(
article.title!,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
)
],
),
),
);
}
let me know if you guys needs to see more of my codes if necessary. thankyou guys
CodePudding user response:
Your listTile
takes two positional argument. 1st one article and the next one is BuildContext
Widget listTile(Article article, BuildContext context) {
You can do
itemBuilder: (context, index) => listTile(articles[index], context)
But I will prefer switching the position and make the context at first. For more prefer required named argument.
CodePudding user response:
Try This
itemBuilder: (context, index) => listTile(articles[index],context)
CodePudding user response:
your listTile widget needs buildContext also change your code to this
itemBuilder: (context, index) => listTile(articles[index],context)
CodePudding user response:
You can use like below
itemBuilder: (BuildContext context, int index) => listTile(articles[index], context)
CodePudding user response:
Okay, since you are new in flutter, I should first give you an advice and then answer your question.
Advice
If you starting with flutter without any previous programming experience, You should let things take time, and first start with programming basics then move to flutter.
Now about Your Question
Describe the problem and the solution
In Programming, We have something called methods or functions, Functions are block of lines code, but separated from main program.
and in your code above,
Widget listTile(Article article, BuildContext context) {
/// body
}
Here, ListTile is simply a function, and
(Article article, BuildContext context)
called function arguments, these arguments need to be passed from out side from where we call out function, in your code you call it here :
itemBuilder: (context, index) => listTile(articles[index])
But, what you missed that, when you create your function you create it and make it taking 2 arguments, first one is "Article", and the second is context,
and when you call it, you forget to pass context to it, so that error shown to you says "2 positional argument(s) expected, but 1 found. "
Final Answer
after adding the missing parameter to function, it will be like this and the error will solved:
itemBuilder: (context, index) => listTile(articles[index])