
class에 list를 만들었는데 불러오는 방법을 아무리 찾아도 모르겠습니다.
제발 알려주세요!
작성한 코드 및 에러 메세지
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => Group(),
)
],
child: MaterialApp(
theme: ThemeData(
useMaterial3: true,
),
home: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({
super.key,
});
@override
Widget build(BuildContext context) {
return Consumer<Group>(
builder: (context, group, child) => Scaffold(
appBar: AppBar(
title: const Text('리스트'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: group.groupList.length,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
color: Colors.grey.shade300,
height: 100,
child: const ListTile(
title: Text('name'), // LIstView에 name과 age를 나오게 하려면 어떻게 하면 되나요?
subtitle: Text('age'),
//title: Text('${Group().groupList[].name}'),
//subtitle: group.groupList[].name,
//subtitle: (Text('groupList age')),
),
),
),
),
),
],
)));
}
}
class Person {
String name;
int age;
Person({
required this.name,
required this.age,
});
}
class Group extends ChangeNotifier {
List<Person> groupList = [
Person(name: '레드', age: 11),
Person(name: '블루', age: 12),
Person(name: '화이트', age: 13),
Person(name: '블랙', age: 14),
Person(name: '퍼플', age: 15),
Person(name: '그레이', age: 16),
];
}
