skip to Main Content

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


  1. 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 when CardWid method is called, the parameters (of type dynamic) are null.

    // Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
    // for details. All rights reserved. Use of this source code is governed by a
    // BSD-style license that can be found in the LICENSE file.
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatelessWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return const Scaffold(body:CardWidget(
          pcolor: Colors.orangeAccent,
          ptext: 'I need team for First Person Shooter',
          paccount: 'gamedev21',
          pdate: 'February 12, 2023, 21:00',
          peoplein: 2,
        ),);
      }
    }
    
    class CardWidget extends StatelessWidget {
      const CardWidget({
        Key? key,
        required this.pcolor,
        required this.ptext,
        required this.paccount,
        required this.pdate,
        required this.peoplein,
      }) : super(key: key);
    
      final Color pcolor;
      final String ptext;
      final String paccount;
      final String pdate;
      final int peoplein;
    
      Widget cardWidget() {
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
          color: pcolor,
          child: Column(
            children: [
              Text("$ptext, $paccount, $pdate, $peoplein"),
            ],
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return cardWidget();
      }
    }
    
    Login or Signup to reply.
  2. import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: HomePage(),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search