Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

Import class object in flutter

I want to use the below Player object in my body_playerDetails.dart file. But I get the following error.

======== Exception caught by widgets library ======================================================= The following _TypeError was thrown building PlayerDataBody(dirty, dependencies: [MediaQuery], state: _PlayerDataBodyState#9804d): type 'List' is not a subtype of type 'List' of 'function result' where Player is from package:chess_mate_app/details_of_players/body_playerDetails.dart Player is from package:chess_mate_app/Player.dart

The relevant error-causing widget was: PlayerDataBody file:///D:/FlutterApps/chessMATE_app/lib/screens/playerDetails_screen.dart:14:13 When the exception was thrown, this was the stack: #0 _PlayerDataBodyState.players (package:chess_mate_app/details_of_players/body_playerDetails.dart) #1 _PlayerDataBodyState.build (package:chess_mate_app/details_of_players/body_playerDetails.dart:191:29) #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28) #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15) #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11) ...

======================================================================================================== Following is my Player.dart file.

class Player {
  final String profileImg;
  final String username;
  final String rating_level;
  final String age;

  Player({this.profileImg, this.username, this.rating_level, this.age});
}

===================================================================================================== This is my body_playerDetails.dart.

import 'package:chess_mate_app/screens/gameScreen.dart';
import 'package:flutter/material.dart';

import '../Player.dart';



class PlayerDataBody extends StatefulWidget {
  @override
  _PlayerDataBodyState createState() => _PlayerDataBodyState();
}

class _PlayerDataBodyState extends State<PlayerDataBody> {

  List<Player> players = [
    Player(
        profileImg: 'assets/player.png',
        username: 'John',
        rating_level: "1",
        age: "25"),
    Player(
        profileImg: 'assets/player.png',
        username: 'CharlesLewis',
        rating_level: "2",
        age: "33"),
    Player(
        profileImg: 'assets/player.png',
        username: 'Brenden',
        rating_level: "3",
        age: "19"),
    Player(
        profileImg: 'assets/player.png',
        username: 'Mathew',
        rating_level: "4",
        age: "30"),
    Player(
        profileImg: 'assets/player.png',
        username: 'PeterRoger',
        rating_level: "5",
        age: "28"),
    Player(
        profileImg: 'assets/player.png',
        username: 'Thomas',
        rating_level: "6",
        age: "20"),
    Player(
        profileImg: 'assets/player.png',
        username: 'Mandy',
        rating_level: "7",
        age: "33"),
    Player(
        profileImg: 'assets/player.png',
        username: 'William',
        rating_level: "8",
        age: "40")
  ];

  Widget personDetailCard(Player) {
    return Padding(
      padding: const EdgeInsets.all(4),
      child: InkWell(
        splashColor: Colors.red,
        onTap: () {
          Navigator.pushNamed(context, GameScreen.id);
        },
        child: Card(
          elevation: 20,
          shape: RoundedRectangleBorder(
              side: new BorderSide(color: Colors.blue, width: 3.0),
              borderRadius: BorderRadius.circular(0)),
          child: Padding(
            padding: const EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                      width: 50.0,
                      height: 50.0,
                      decoration: new BoxDecoration(
                          shape: BoxShape.circle,
                          image: new DecorationImage(
                              fit: BoxFit.cover,
                              image: AssetImage(Player.profileImg)))),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      Player.username,
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 20,
                        fontFamily: "Acme",
                        letterSpacing: 2,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          "Rating Level: ",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                            fontFamily: "Acme",
                            letterSpacing: 2,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Text(
                          Player.rating_level,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                            fontFamily: "Acme",
                            letterSpacing: 2,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          "Age: ",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                            fontFamily: "Acme",
                            letterSpacing: 2,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Text(
                          Player.age,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                            fontFamily: "Acme",
                            letterSpacing: 2,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return SingleChildScrollView(
      child: Container(
        child: Padding(
          padding: const EdgeInsets.fromLTRB(10, 50, 10, 10),
          child: Column(
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Image.asset(
                    "assets/logo.png",
                    height: size.height * 0.2,
                  ),
                  SizedBox(
                    height: size.height * 0.05,
                  ),
                  Text(
                    "FIND PLAYERS",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 30,
                      color: Colors.white,
                      fontFamily: "Acme",
                      letterSpacing: 7,
                    ),
                  ),
                ],
              ),
              Column(
                  children: players.map((p) {
                    return personDetailCard(p);
                  }).toList()),
            ],
          ),
        ),
      ),
    );
  }
}
question from:https://stackoverflow.com/questions/65903990/import-class-object-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...