How can I Remove an error While Defining return type as VoidCallback of a property? - flutter

I get an error Whenever I try to use VoidCallback instead of function when i Define GetAnswer.I am using it as a pointer to get value in it in the constructor below.But I am continuously getting an error shown in the 2nd pic.Seniors Plz Guide
import 'dart:html';
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback GetAnswer;
Answer(this.GetAnswer);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.blue,
child: Text("4 CGPA"),
onPressed: GetAnswer,
),
);
}
}

Remove the import import 'dart:html';
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback GetAnswer;
Answer(this.GetAnswer);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.blue,
child: Text("4 CGPA"),
onPressed: GetAnswer,
),
);
}
}

Related

The argument type 'Function' can't be assigned to the parameter type 'void Function()? this error is shown in onpressed section

import 'package:flutter/material.dart';
class Answer extends StatelessWidget{
final Function selectHandler;
Answer(this.selectHandler);
Widget build(BuildContext context){
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.green,
child: Text('Answer1'),
onPressed: selectHandler,
),
);
}
}

error in Custome class with required and not required fields

I am creating a class where I want some fields to be required and some not required but I am getting an error and it forced me to make it required where I don't require it
I have made simple demo to explain my problem...
where I want to make a container with gesturedetector and a text ..Text is required but don't want to make required function
feeling bad that I am failing to solve such simple coding...
here is my coding
import 'package:flutter/material.dart';
import 'mybutton.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body:Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MyButton(txt: 'red',function: (){
print('red is clicked');
}),
MyButton(txt: 'Green'),
],
),
),
),
);
}
}
here is my custome class
import 'package:flutter/material.dart';
class MyButton extends StatelessWidget {
final String txt;
final VoidCallback function;
MyButton({required this.txt, this.function});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: function,
child: Container(
height: 60,
width: 100,
color: Colors.blue,
child: Text(txt,style: TextStyle(fontSize: 30.0,color: Colors.green),),
),
);
}
}
Dart enforces you all the time, to have non null values for all of your variables, unless they are marked as nullable.
Changing your code to this, will fix your issue:
final VoidCallback? function;
The question mark tells Dart, that your function variable can have a null value, hence the constructor will work as it currently is.

Error: Exception has occurred. ProviderNotFoundException (Error: Could not find the correct Provider<List<FBUser>> above this CustList Widget

The error happened when I created the 2 dart files below and I have reviewed my code over and over and I still can't find a solution to the error. If anyone can explain where I went wrong and how I can fix the error it would be much appriciated.
I can provide more code or more files if any of the code down below isn't clear.
cust_list.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:subitemtracker/models/user.dart';
import 'package:subitemtracker/screens/home/cust_tile.dart';
class CustList extends StatefulWidget {
#override
_CustListState createState() => _CustListState();
}
class _CustListState extends State<CustList> {
#override
Widget build(BuildContext context) {
// error is on the line of code below
final custs = Provider.of<List<FBUser>>(context); //(add double
"??", do this on home.dart too)
return ListView.builder(
itemCount: custs!.length,
itemBuilder: (context, index) {
return CustTile(cust: custs[index]);
},
);
}
}
cust_tile.dart
import 'package:flutter/material.dart';
import 'package:subitemtracker/models/user.dart';
class CustTile extends StatelessWidget {
final FBUser cust;
CustTile({ required this.cust });
#override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 8.0),
child: Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0),
child: ListTile(
leading: CircleAvatar(
radius: 25.0,
backgroundColor: Colors.black,
),
title: Text(cust.uid),
subtitle: Text('First subscription'),
),
)
);
}
}

The named parameter 'textColor' isn't defined

I'm getting this error when writing for example color or textColor maybe even with more.
I tried:
flutter clean -> everything is now red, even after restarting. I have to use
'flutter pub get' to get it back to normal
restating PC
ctrl + shift + p -> Dart: Restart Analysis Server
Nothing changes
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback selectHandler;
Answer(this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
textColor: Colors.green,
child: Text('Answer 1'),
onPressed: selectHandler,
),
);
}
}
I have no idea what to do and I'm starting to hate this language more and more
Some help would really be appreciated
Do this:
class Answer extends StatelessWidget {
final VoidCallback selectHandler;
Answer(this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
child: Text('Answer 1', style: TextStyle(color: Colors.green)),
onPressed: selectHandler,
),
);
}
}

Error: The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.'Function' is from 'dart:core'.onPressed: selectHandler

main.dart
import 'package:flutter/material.dart';
import './questions.dart';
import './answer.dart';
void main() {
runApp(AskMe());
}
class AskMe extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _AskMeState();
}
}
class _AskMeState extends State<AskMe> {
var _next_ques = 0;
void _Response() {
setState(() {
_next_ques += 1;
});
print(_next_ques);
}
#override
Widget build(BuildContext context) {
var questions = ["What is your Name ?", "What is your favourite color ?"];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Ask Me"),
),
body: Column(
children: [
Questions(questions[_next_ques]),
Answer(_Response),
Answer(_Response),
Answer(_Response),
],
),
),
);
}
}
questions.dart
import 'package:flutter/material.dart';
class Questions extends StatelessWidget {
final String questions;
Questions(this.questions);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(20),
child: Text(
questions,
style: TextStyle(fontSize: 28),
textAlign: TextAlign.center,
),
);
}
}
answer.dart
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final Function selectHandler;
Answer(this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(10),
child: RaisedButton(
color: Colors.blueGrey,
child: Text("Question 1"),
onPressed: selectHandler,
),
);
}
}
The above code is for basic app that consists of 3 buttons for the questions shown as textview and clicking on button will display next question. The issue i'm facing is while making custom dart function in answer.dart i'm not able to call the pointer to the function _Response which is present in main.dart as it gives the error mentioned in the title. Thanks for the help.
Define function type like this:
class Answer extends StatelessWidget {
final Function() selectHandler;
...