Flutter:如何为展开列表项设置不同价格变量并解决类型错误
Let's fix this error and get your ExpansionTiles showing custom prices as intended!
What's Causing the Error?
Your MyTile class constructor is defined to take price (an int) as the first parameter, followed by title (a String), then optional children:
MyTile(this.price,this.title, [this.children = const <MyTile>[]]);
But when you create MyTile instances, you're passing the title string first (like 'Gin Cocktails') — that means you're trying to assign a string to the price variable (which expects an integer), hence the type mismatch error. On top of that, most of your child tiles don't have a price value passed at all.
Step-by-Step Fix
Adjust Constructor Parameter Order (For Intuition)
Swap the order oftitleandpricein theMyTileconstructor so it matches how you're actually writing your tile data. We'll also makepricenullable since parent expansion tiles don't need to show a price:MyTile(this.title, {this.price, this.children = const <MyTile>[]});Using named parameters for
priceandchildrenmakes the code clearer and avoids order mix-ups.Assign Proper Integer Prices to Child Tiles
Go through yourlistOfTilesand add integer price values to each child tile that needs to display a price. Parent tiles (the ones that expand) can omit the price parameter.Update the ListTile Subtitle Logic
In the_buildTilesmethod, only show the subtitle ifpriceis not null (so parent expansion tiles don't display an empty or 0 price).
Full Corrected Code
import 'package:flutter/material.dart'; void main() { runApp(new TileApp()); } class TileApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, home: new Scaffold( appBar: new AppBar( title: new Center( child: Text('ExpansionTile App'), ), actions: <Widget>[ Icon(Icons.shopping_basket, size:35) ], ), body: new ListView.builder( itemBuilder: (BuildContext context, int index) { return new StuffInTiles(listOfTiles[index]); }, itemCount: listOfTiles.length, ), ), ); } } class StuffInTiles extends StatelessWidget { final MyTile myTile; StuffInTiles(this.myTile); @override Widget build(BuildContext context) { return _buildTiles(myTile); } Widget _buildTiles(MyTile t) { if (t.children.isEmpty) { return new ListTile( dense: true, enabled: true, isThreeLine: false, onLongPress: () => print("long press"), onTap: () => print("tap"), // Only show subtitle if price exists subtitle: t.price != null ? new Text("R${t.price.toString()}") : null, leading: new Text("Leading"), selected: true, trailing: GestureDetector(child: Icon(Icons.add, color: Colors.green)), title: new Text(t.title) ); } return new ExpansionTile( key: new PageStorageKey<int>(3), title: new Text(t.title), children: t.children.map(_buildTiles).toList(), ); } } class MyTile { String title; int? price; // Make price nullable List<MyTile> children; // Updated constructor with named parameters for clarity MyTile(this.title, {this.price, this.children = const <MyTile>[]}); } List<MyTile> listOfTiles = <MyTile>[ new MyTile( 'Gin Cocktails', children: <MyTile>[ new MyTile('Clover Club', price: 24), new MyTile('Tanqueray Sling', price: 28), new MyTile('1934 Cosmo', price: 30), ], ), new MyTile( 'Beers & Ciders', children: <MyTile>[ new MyTile('Heineken NRB', price: 18), new MyTile('Corona', price: 22), ], ), new MyTile( 'Wine List', children: <MyTile>[ new MyTile('Wine By The Glass', price: 25), new MyTile('White Wine', price: 32), new MyTile( 'Red Wine', children: <MyTile>[ new MyTile('Spier Signature Merlot', price: 35), new MyTile('Glenelly Glass Collection', price: 40), new MyTile('Nederburg Baronnen ', price: 38), new MyTile('Nero D’Avola ', price: 36), new MyTile('Reyneke Organic Cab Sav/Merlot ', price: 42), ], ), ], ), ];
Key Improvements
- No more type mismatch errors because we're passing integer prices where expected
- Parent expansion tiles don't show a price (cleaner UI)
- Named parameters make the tile creation code easier to read and maintain
- Subtitle now formats the price with the "R" prefix as you had originally
内容的提问来源于stack exchange,提问作者Lefentse Mphahlele

