前提
FlutterのUI構築の仕方を勉強するにあたり、Youtubeの動画を真似して勉強しています。
実現したいこと
- Try renaming one of the declarations.を解決させて正常な挙動にする。
発生している問題・エラーメッセージ
The name 'press' is already defined. Try renaming one of the declarations.
該当のソースコード
body.dart
1import 'package:flutter/material.dart'; 2import 'package:flutter_svg/svg.dart'; 3import 'package:plant_app/constants.dart'; 4 5import 'header_with_searchbox.dart'; 6import 'recomend_plants.dart'; 7import 'title_with_more_bbtn.dart'; 8 9class Body extends StatelessWidget { 10 const Body({super.key}); 11 12 @override 13 Widget build(BuildContext context) { 14 Size size = MediaQuery.of(context).size; 15 return SingleChildScrollView( 16 child: Column( 17 children: <Widget>[ 18 HeaderWithSearchbox(size: size), 19 TitleWithMoreBtn(title: "Recomended", press: () {}), 20 RecomendsPlants(), 21 TitleWithMoreBtn(title: "Featured Plants", press: () {}), 22 Row( 23 children: <Widget>[ 24 FeaturePlantCard( 25 image: 26 "/Users/ユーザー名/Flutter/plant_app/plant_app/assets/images/bottom_img_1.png", 27 press: () {}, 28 size: null, 29 ), 30 ], 31 ) 32 ], 33 ), 34 ); 35 } 36} 37 38class FeaturePlantCard extends StatelessWidget { 39 const FeaturePlantCard({ 40 Key? key, 41 required this.size, 42 required this.image, 43 required Function() press, 44 required this.press, 45 }) : super(key: key); 46 47 final Size? size; 48 final String image; 49 final void Function() press; 50 51 @override 52 Widget build(BuildContext context) { 53 Size size = MediaQuery.of(context).size; 54 return GestureDetector( 55 onTap: press, 56 child: Container( 57 margin: EdgeInsets.only( 58 left: kDefaultPadding, 59 top: kDefaultPadding / 2, 60 bottom: kDefaultPadding / 2, 61 ), 62 width: size.width * 0.8, 63 height: 185, 64 decoration: BoxDecoration( 65 borderRadius: BorderRadius.circular(10), 66 image: DecorationImage( 67 fit: BoxFit.cover, 68 image: AssetImage(image), 69 ), 70 ), 71 ), 72 ); 73 } 74} 75
試したこと
The name 'press' is already defined. Try renaming one of the declarations.を和訳したところ、
名前 'press' はすでに定義されています。宣言の一つをリネームしてみてください。という意味だと判明。
The name 'press' is already defined. Try renaming one of the declarations.でググったところ、以下のページに行き着く。
https://stackoverflow.com/questions/20598170/error-default-constructor-is-already-defined
Dartでは同じメソッド名、コンストラクタ名を複数回使用することはできないということは分かりましたが現在の自分のレベルはFlutterでのUI構築のやり方を勉強するという段階なのでpressをリネームすると言っても、リネームした先でまたエラーが吐かれてしまっている状況なので、訳がわからなくなってしまっている状況です。
補足情報(FW/ツールのバージョンなど)
Youtubeの動画
https://www.youtube.com/watch?v=LN668OAUrK4

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/08 14:20