select provider 질문있습니다!
- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 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.dart
import '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.dart
class 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,
);
}
}어느 부분이 잘못되었을까요...?
답변 1
0
아 토글 함수 만들때 state.copyWith( hasBought: !state.hasBought)를 state에 재 할당해주지 않아서 생긴 문제였네요.... 해결했습니다 ㅠㅠ 죄송해요
state = state.copyWith( hasBought: !state.hasBought); 이렇게 해주니 해결되었습니다!
Isar 마지막 업데이트는 2년전입니다.
0
28
0
FlutterSecureStorage 질문
0
27
0
Dio onError Interceptor 만드는 부분에 질문이 있습니다.
0
75
2
관리자 기능에 대한 질문
0
97
2
part 'restaurant_model.g.dart';
0
91
1
36강. dio 인터셉터에 storage를 전달하는 코드가 이해 안되는데요. 도움 부탁드립니다.
0
54
2
2번 반환 상황 관련 질문
0
58
2
riverpod 3.0
0
139
2
Asset folder??
0
79
2
디자이너와 협업 시 프레임 크기 설정 관련 질문
0
111
2
FutureProvider, StateNotifierProvider 선택 기준
0
68
2
컴포넌트 모델화
0
61
2
쿼리 파라미터
0
81
2
화면 안보임
0
64
2
PaginationListView
0
50
1
강의중 37.Dio onErrorInterceptor 작업하기 dio 관련 질문입니다.
0
99
2
프로토타입이미지
0
59
2
여러 객체를 상태 관리하는 방법에 대한 질문
0
82
2
장바구니 결제하기 응답이 500이 옵니다.
0
102
2
removeFromBasket에서 await patchBasket()을 마지막에 하면 에러나는거 아닌가요?
0
64
2
이 두가지는 완전히 동일한 기능인가요?
0
104
3
내부 코드를 작성하지 않은 CursorPaginationLoading가 어떻게 로딩상태를 갖는지 잘 모르겠습니다...
0
75
2
_SplashScreenState에서 storage를 late로 호출해서 한번만 불러와도 되나요?
0
84
2
코딩 작성 순서 관련 질문
0
86
2





