강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của jia52323719
jia52323719

câu hỏi đã được viết

[Nhà máy mã] [Trung cấp] Hoạt động rung chuyển! Các kỹ năng cần thiết để trở thành tiền vệ như quản lý trạng thái, quản lý bộ đệm, tạo mã, GoRouter và logic xác thực!

Chọn thực hành

select provider 질문있습니다!

Đã giải quyết

Viết

·

343

0

- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.

코드팩토리 디스코드
https://bit.ly/3HzRzUM

Flutter 강의를 구매하시면 코드팩토리 디스코드 서버 플러터 프리미엄 채널에 들어오실 수 있습니다! 디스코드 서버에 들어오시고 저에게 메세지로 강의를 구매하신 이메일을 보내주시면 프리미엄 채널에 등록해드려요! 프리미엄 채널에 들어오시면 모든 질의응답 최우선으로 답변해드립니다! 강의에 나오는 코드와 동일하다고 생각하는데 SelectProviderScreen을 띄웠을 때 버튼을 눌러서 toggle 메소드들이 실행되게 해도 값이 변경이 안됩니다! ㅠㅠ

select_provider_screen.dart
class SelectProviderScreen extends ConsumerWidget { const SelectProviderScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final state = ref.watch(selectProvider); return DefaultLayout( title: 'SelectProviderScreen', body: SizedBox( width: double.infinity, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(state.name), Text(state.isSpicy.toString()), Text(state.hasBought.toString()), ElevatedButton( onPressed: () { ref.read(selectProvider.notifier).toggleIsSpicy(); }, child: Text('Spicy Toggle'), ), ElevatedButton( onPressed: () { ref.read(selectProvider.notifier).toggleHasBought(); }, child: Text('hasBought Toggle'), ), ], ), ), ); } }
select_provider.dartimport 'package:advanced_state/model/shopping_item_model.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; final selectProvider = StateNotifierProvider<SelectNotifier, ShoppingItemModel>( (ref) => SelectNotifier(), ); class SelectNotifier extends StateNotifier<ShoppingItemModel> { SelectNotifier() : super( ShoppingItemModel( name: '김치', quantity: 3, hasBought: false, isSpicy: true, ), ); toggleHasBought() { state.copyWith( hasBought: !state.hasBought, ); } toggleIsSpicy() { state.copyWith( isSpicy: !state.isSpicy, ); } }
shopping_item_model.dartclass ShoppingItemModel { // 이름 final String name; // 갯수 final int quantity; // 구매했는지 final bool hasBought; // 매운지 final bool isSpicy; ShoppingItemModel({ required this.name, required this.quantity, required this.hasBought, required this.isSpicy, }); ShoppingItemModel copyWith({ //선택한 값만 선택적으로 변경할 수 있게 메소드를 만든다. String? name, int? quantity, bool? hasBought, bool? isSpicy, }) { return ShoppingItemModel( name: name ?? this.name, //name이 null일때는 this.name, null이 아닐때는 입력받은 name이 된다. quantity: quantity ?? this.quantity, hasBought: hasBought ?? this.hasBought, isSpicy: isSpicy ?? this.isSpicy, ); } }
어느 부분이 잘못되었을까요...?
flutter하이브리드-앱

Câu trả lời 1

0

star님의 프로필 이미지
star
Người đặt câu hỏi

아 토글 함수 만들때 state.copyWith( hasBought: !state.hasBought)를 state에 재 할당해주지 않아서 생긴 문제였네요.... 해결했습니다 ㅠㅠ 죄송해요

state = state.copyWith( hasBought: !state.hasBought); 이렇게 해주니 해결되었습니다!

Hình ảnh hồ sơ của jia52323719
jia52323719

câu hỏi đã được viết

Đặt câu hỏi