I created a class that stores widget named CardWidget with parameters. I stored it in different file to use it multiple times.
CardWidget is a Widget that contains text data like title, description,date and account. I need it to create special boards in home page like it used to be in different forums.
.Widget file
import 'package:flutter/material.dart';
class CardWidget extends StatelessWidget {
const CardWidget({Key? key, pcolor, ptext, paccount, pdate, peoplein}) : super(key: key);
Widget CardWid({pcolor, ptext, paccount, pdate, peoplein}) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 5),
// data was erased for economy
child: Container());
}
@override
Widget build(BuildContext context) {
return CardWid();
}
}
When i call it from another flutter class it returns an issue.
type ‘Null’ is not a subtype of type ‘String’
.main file, where i call the widget and give parameters
class HomePageState extends State<HomePage> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
CardWidget(
pcolor: Colors.orangeAccent,
ptext: 'I need team for First Person Shooter',
paccount: 'gamedev21',
pdate: 'February 12, 2023, 21:00',
peoplein: 2),
CardWidget(
pcolor: Colors.green[700],
ptext: 'text',
paccount: 'text',
pdate: 'February 12, 2023, 18:00',
peoplein: 4),
],
),
);
}
2
Answers
You should type your parameters in the constructor and define them in the Widget itself, like so
Your issue exists because the values you pass to the
CardWidgets
‘s constructor are not actually being saved anywhere, so whenCardWid
method is called, the parameters (of typedynamic
) are null.