i want to pass postid value from a statefulwidget to a function inside Providerclass, somebody please help
class CommentPage extends StatefulWidget {
final String postid;
final String postdescription;
CommentPage({
required this.postdescription,
Key? key,
required this.postid,
}) : super(key: key);
#override
State<CommentPage> createState() => _CommentPageState();
}
Providerclass
class CommentPageProvider extends ChangeNotifier {
static CommentPageProvider instance = CommentPageProvider();
List<HomeFeedDataModel> homeFeedDatacollection = [];
List<CommentsModelData> commentsDatacollection = [];
List<SingleUserPostModelData> singleuserdatacollection = [];
bool isLoadingdata = false;
bool isSingleUserdata = false;
bool isLikePressed = false;
bool isLoadCommentsPressed = false;
bool isLikeButtonPressed = false;
bool isCommentsDataloaded = false;
getCommentsData() async {
isCommentsDataloaded = true;
commentsData = await CommentService().getCommentsdata(post_id: );
notifyListeners();
}
so that i can fetch matching comments,
What's wrong with passing the post ID into the getCommentsData() as a parameter?
class CommentPageProvider extends ChangeNotifier {
static CommentPageProvider instance = CommentPageProvider();
List<HomeFeedDataModel> homeFeedDatacollection = [];
List<CommentsModelData> commentsDatacollection = [];
List<SingleUserPostModelData> singleuserdatacollection = [];
bool isLoadingdata = false;
bool isSingleUserdata = false;
bool isLikePressed = false;
bool isLoadCommentsPressed = false;
bool isLikeButtonPressed = false;
bool isCommentsDataloaded = false;
getCommentsData(String postId) async {
// I changed the name of this because there is no 'commentsData' field in your provider class
commentsDatacollection = await CommentService().getCommentsdata(post_id: postId);
isCommentsDataloaded = true;
notifyListeners();
}
By declaring your data collection as nullable (List<CommentsModelData> commentsDatacollection = [];), I think you could remove the boolean isCommentsDataloaded and simply check for a null value in your widget build method.
CommentPage
Here you just access the provider and pass through the post ID.
class CommentPage extends StatefulWidget {
final String postid;
final String postdescription;
CommentPage({
required this.postdescription,
Key? key,
required this.postid,
}) : super(key: key);
#override
State<CommentPage> createState() => _CommentPageState();
}
class _CommentPageState extends State<CommentPage> {
#override
Widget build(BuildContext context) {
final provider = Provider.of<CommentPageProvider>(context).getCommentsData(widget.postId);
}
}
Let me know if this helped!
Related
How to solve this error?
The named parameter 'key' is required, but there's no corresponding argument. (Documentation) Try adding the required argument.
error
Future<void> onJoin() async {
// update input validation
setState(() {
_channelController.text.isEmpty
? _validateError = true
: _validateError = false;
});
if (_channelController.text.isNotEmpty) {
await _handleCameraAndMic(Permission.camera);
await _handleCameraAndMic(Permission.microphone);
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VideoCall(
channelName: _channelController.text,
role: _role,
),
),
);
}
}
class VideoCall
class VideoCall extends StatefulWidget {
final String channelName;
final ClientRole role;
const VideoCall({Key key, required this.channelName, required this.role})
: super(key: key);
#override
_VideoCallState createState() => _VideoCallState();
}
class _VideoCallState extends State<VideoCall> {
final _users = <int>[];
final _infoStrings = <String>[];
bool muted = false;
late RtcEngine _engine;
#override
void dispose() {
// clear users
_users.clear();
// destroy sdk
_engine.leaveChannel();
_engine.destroy();
super.dispose();
}
#override
void initState() {
super.initState();
// initialize agora sdk
initialize();
}
this is the videoCall class in there no any error shows.
when add "key" show this
When remove required property from key in video call class
show this error
In VideoCall class, key property set as a required, change it to optional:
class VideoCall extends StatefulWidget {
final String? channelName;
final ClientRole? role;
const VideoCall({Key? key, this.channelName, this.role})
: super(key: key);
#override
_VideoCallState createState() => _VideoCallState();
}
I am getting bool value timerState through the constructor. When I print the value I get proper results (true or false).
I need to call the function _controller.start() when timerState() == true. However when it is true I get the error: "Failed assertion: boolean expression must not be null".
Surprisingly, when timerState is false, I am not getting this error.
Please help, where can be a problem.
Below is my code:
class PieChart extends StatefulWidget {
final String userId;
final String userName;
final bool timerState;
final Key key;
PieChart(
this.userId,
this.userName,
this.timerState,
{this.key});
#override
State<StatefulWidget> createState() => PieChartEmotionsState(
userId,
userName,
timerState,
);
}
class PieChartEmotionsState extends State {
final String userId;
final String userName;
final bool timerState;
final Key key;
PieChartEmotionsState(
this.userId,
this.userName,
this.timerState,
{this.key});
#override
Widget build(BuildContext context) {
CountDownController _controller = CountDownController();
int _duration = 10;
bool getTimerState() {
print('PIE CHART timerState: $timerState');
return timerState == true;
}
if (getTimerState()) {
_controller.start();
}
return Container(...
I have obviously tried a simpler way:
if(timerState) _controller.start();
even this:
if(timerState ?? false) _controller.start();
But it seems not to have any impact on the issue. I am getting the same error...
What could be the solution?
Many thanks in advance for your support.
Dan
You don't need co create copies of your widget classes variables in the State class. Access them as widget.variable
class PieChart extends StatefulWidget {
final String userId;
final String userName;
final bool timerState;
final Key key;
PieChart(
this.userId,
this.userName,
this.timerState,
{this.key});
#override
State<PieChart> createState() => _PieChartState();
}
class _PieChartState extends State<PieChart> {
bool getTimerState() {
print('PIE CHART timerState: ${widget.timerState}');
return widget.timerState == true;
}
#override
Widget build(BuildContext context) {
CountDownController _controller = CountDownController();
int _duration = 10;
if (getTimerState()) {
_controller.start();
}
return Container(...
and make sure you initialize timerState when creating your PieChart widget as it's declared final.
PieChart('userId', 'userName', true);
If you don't initialize it, it's set to null and the error you get is because you are checking if null == true in your getTimerState() method.
I try to pass data from page one to page two data is pass OK but I have one problem now.
this is my code:
class SecondScreen extends StatefulWidget {
final int itemHolder ;
SecondScreen({Key key, #required this.itemHolder}) : super(key: key);
#override
State<StatefulWidget> createState() {
return new mainState();
}
}
class mainState extends State <SecondScreen> {
bool value = false ;
MyPreferences _myPreferences = MyPreferences();
#override
void initState() {
// TODO: implement initState
super.initState();
initial();
}
void initial() async {
setState(() {
});
}
final String apiURL = 'http://xxxxxxxxx/getFlowersList.php';
Future<List<Flowerdata>> fetchFlowers() async {
var response = await http.get(apiURL);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<Flowerdata> listOfFruits = items.map<Flowerdata>((json) {
return Flowerdata.fromJson(json);
}).toList();
return listOfFruits;
}
else {
throw Exception('Failed to load data from Server.');
}
}
I try to use var (itemHolder ) in the link like that:
final String apiURL = 'http://xxxxxxx/getFlowersList.php?id=' +itemHolder;
but I get error:
Undefined name 'itemHolder'. Try correcting the name to one that is defined, or defining the name.
I can access to itemHolder. So how can I access to it?
try this:
...
String apiURL;
#override
void initState() {
super.initState();
apiURL = 'http://xxxxxxx/getFlowersList.php?id=' +widget.itemHolder.toString();
}
...
To use the variables declared in the SecondScreen you have to access it with the prefix 'widget' and the dot operator, not directly. Here is the code:
final String apiURL = 'http://xxxxxxx/getFlowersList.php?id=' + widget.itemHolder;
And when you make the variable final, you have to initialize it while declaration or through constructor else delete keyword final:
I tried to use $url inside lodfromurl() function but I cant defined $url inside lodfromurl() ! can you help me please ?
Package Used flutter_plugin_pdf_viewer
The full code is :
import 'package:example/Home/booklist.dart';
import 'package:flutter/material.dart';
import 'package:flutter_plugin_pdf_viewer/flutter_plugin_pdf_viewer.dart';
class PDFViwer extends StatefulWidget {
final String url;
PDFViwer({Key key, #required this.url}) : super(key: key);
#override
_PDFViwerState createState() => _PDFViwerState();
}
class _PDFViwerState extends State<PDFViwer> {
#override
void initState() {
// TODO: implement initState
super.initState();
lodfromurl();
}
PDFDocument document;
bool _isLoading = true;
lodfromurl()async{
document = await PDFDocument.fromURL($url);
setState(() {
_isLoading = false;
});
}
}
Url isn't defined in your state class so you have to use widget. to access variable.
import 'package:example/Home/booklist.dart';
import 'package:flutter/material.dart';
import 'package:flutter_plugin_pdf_viewer/flutter_plugin_pdf_viewer.dart';
class PDFViwer extends StatefulWidget {
final String url;
PDFViwer({Key key, #required this.url}) : super(key: key);
#override
_PDFViwerState createState() => _PDFViwerState();
}
class _PDFViwerState extends State<PDFViwer> {
#override
void initState() {
// TODO: implement initState
super.initState();
lodfromurl();
}
PDFDocument document;
bool _isLoading = true;
lodfromurl()async{
document = await PDFDocument.fromURL(widget.url);
setState(() {
_isLoading = false;
});
}
}
I want to get the value of latitude and longitude variables, so that I can pass them to the fetchAlbum() function inside initstate().
class MainContent extends StatefulWidget {
final double latitude;
final double longitude;
MainContent({Key key, #required this.latitude, #required this.longitude}) : super(key: key);
#override
_MainContentState createState() => _MainContentState();
}
class _MainContentState extends State<MainContent> {
bool isLoading = false;
Future<Album> futureAlbum;
#override
void initState() {
super.initState();
var a = 23.2599; // update the value of 'a' by using 'latitude' variable
var b = 77.4126; // update the value of 'a' by using 'longitude' variable
futureAlbum = fetchAlbum(a, b);
}
Do it like this:
var a = widget.latitude;
var b = widget.longitude;