質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

Q&A

解決済

1回答

998閲覧

The name 'press' is already defined. Try renaming one of the declarations.

ituking

総合スコア80

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

0グッド

0クリップ

投稿2023/01/04 15:46

編集2023/01/09 14:11

前提

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FeaturePlantCardのコンストラクタでpressという名前付きパラメータの名称が重複しているからでは。
メンバーとしてpressを持っているので、required Function() press,側を削除すればいいのでは。

dart

1class FeaturePlantCard extends StatelessWidget { 2 const FeaturePlantCard({ 3 Key? key, 4 required this.size, 5 required this.image, 6 required this.press, 7 }) : super(key: key); 8

投稿2023/01/04 23:55

ta.fu

総合スコア1667

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ituking

2023/01/08 14:20

そうですね。エラーメッセージがThe name 'press' is already defined. Try renaming one of the declarations.として吐かれているので、pressのどれかを消すのかなというところまでは特定できたのですが、確定ではなかったので有識者の方に聞いた方が良いなという判断でした。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問